Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EditorFactory.js « src - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e95cd4ff59957ea486c3e750c284127b794f1b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
 *
 * @author Julius Härtl <jus@bitgrid.net>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
*/

/* eslint-disable import/no-named-as-default */
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import History from '@tiptap/extension-history'
import Blockquote from '@tiptap/extension-blockquote'
import Placeholder from '@tiptap/extension-placeholder'
import OrderedList from '@tiptap/extension-ordered-list'
import ListItem from '@tiptap/extension-list-item'
import CodeBlock from '@tiptap/extension-code-block'
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import Dropcursor from '@tiptap/extension-dropcursor'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
import Table from './nodes/Table'
import TableCell from './nodes/TableCell'
import TableHeader from './nodes/TableHeader'
import TableHeadRow from './nodes/TableHeadRow'
import TableRow from './nodes/TableRow'
/* eslint-enable import/no-named-as-default */

import { Editor } from '@tiptap/core'
import { Strong, Italic, Strike, Link, Underline } from './marks'
import {
	Image,
	PlainTextDocument,
	TrailingNode,
	Heading,
	BulletList,
	TaskList,
	TaskItem,
	Callout,
} from './nodes'
import { HardBreak, Markdown, Emoji } from './extensions'
import { translate as t } from '@nextcloud/l10n'
import { listLanguages, registerLanguage } from 'lowlight/lib/core'
import { emojiSearch } from '@nextcloud/vue/dist/Functions/emoji'
import { VueRenderer } from '@tiptap/vue-2'
import EmojiList from './components/EmojiList'
import tippy from 'tippy.js'

import 'proxy-polyfill'

const loadSyntaxHighlight = async (language) => {
	const list = listLanguages()
	console.info(list)
	if (!listLanguages().includes(language)) {
		try {
			const syntax = await import(/* webpackChunkName: "highlight/[request]" */'highlight.js/lib/languages/' + language)
			registerLanguage(language, syntax.default)
		} catch (e) {
			// No matching highlighing found, fallback to none
			console.debug(e)
		}
	}
}

const createEditor = ({ content, onCreate, onUpdate, extensions, enableRichEditing, currentDirectory }) => {
	let richEditingExtensions = []
	if (enableRichEditing) {
		richEditingExtensions = [
			Markdown,
			Document,
			Paragraph,
			HardBreak,
			Heading,
			Strong,
			Italic,
			Strike,
			Link.configure({ openOnClick: true }),
			Blockquote,
			CodeBlock,
			BulletList,
			HorizontalRule,
			OrderedList,
			ListItem,
			Table,
			TableCell,
			TableHeader,
			TableHeadRow,
			TableRow,
			TaskList,
			TaskItem,
			Callout,
			Underline,
			Image.configure({ currentDirectory, inline: true }),
			Emoji.configure({
				suggestion: {
					items: ({ query }) => {
						return emojiSearch(query)
					},
					render: () => {
						let component
						let popup

						return {
							onStart: props => {
								component = new VueRenderer(EmojiList, {
									parent: this,
									propsData: props,
								})

								popup = tippy('body', {
									getReferenceClientRect: props.clientRect,
									appendTo: () => document.body,
									content: component.element,
									showOnCreate: true,
									interactive: true,
									trigger: 'manual',
									placement: 'bottom-start',
								})
							},

							onUpdate(props) {
								component.updateProps(props)
								popup[0].setProps({
									getReferenceClientRect: props.clientRect,
								})
							},

							onKeyDown(props) {
								if (props.event.key === 'Escape') {
									popup[0].hide()
									return true
								}
								return component.ref?.onKeyDown(props)
							},

							onExit() {
								popup[0].destroy()
								component.destroy()
							},
						}
					},
				},
			}),
			Placeholder.configure({
				emptyNodeClass: 'is-empty',
				placeholder: t('text', 'Add notes, lists or links …'),
				showOnlyWhenEditable: true,
			}),
			Dropcursor,
			TrailingNode,
		]
	} else {
		richEditingExtensions = [
			PlainTextDocument,
			CodeBlockLowlight,
		]
	}
	extensions = extensions || []
	return new Editor({
		content,
		onCreate,
		onUpdate,
		extensions: [
			Text,
			History,
			...richEditingExtensions,
		].concat(extensions),
	})
}

const SerializeException = function(message) {
	this.message = message
}

const serializePlainText = (tiptap) => {
	const doc = tiptap.getJSON()

	if (doc.content.length !== 1 || typeof doc.content[0].content === 'undefined' || doc.content[0].content.length !== 1) {
		if (doc.content[0].type === 'code_block' && typeof doc.content[0].content === 'undefined') {
			return ''
		}
		throw new SerializeException('Failed to serialize document to plain text')
	}
	const codeBlock = doc.content[0].content[0]
	if (codeBlock.type !== 'text') {
		throw new SerializeException('Failed to serialize document to plain text')
	}
	return codeBlock.text
}

export default createEditor
export { createEditor, serializePlainText, loadSyntaxHighlight }