-
Notifications
You must be signed in to change notification settings - Fork 94
/
EditorFactory.js
83 lines (72 loc) · 2.08 KB
/
EditorFactory.js
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
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import MentionSuggestion from './components/Suggestion/Mention/suggestions.js'
import 'proxy-polyfill'
import { Editor } from '@tiptap/core'
import { createLowlight } from 'lowlight'
import hljs from 'highlight.js/lib/core'
import { logger } from './helpers/logger.js'
import { FocusTrap, Mention, PlainText, RichText } from './extensions/index.js'
// eslint-disable-next-line import/no-named-as-default
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
const lowlight = createLowlight()
const loadSyntaxHighlight = async (language) => {
const list = hljs.listLanguages()
logger.debug('Supported languages', { list })
if (!lowlight.listLanguages().includes(language)) {
try {
logger.debug('Loading language', language)
// eslint-disable-next-line n/no-missing-import
const syntax = await import(/* webpackChunkName: "highlight/[request]" */`../node_modules/highlight.js/lib/languages/${language}.js`)
lowlight.register(language, syntax.default)
} catch (error) {
// fallback to none
logger.debug('No matching highlighing found', { error })
}
}
}
const editorProps = {
scrollMargin: 50,
scrollThreshold: 50,
}
const createRichEditor = ({ extensions = [], session, relativePath, isEmbedded = false } = {}) => {
return new Editor({
editorProps,
extensions: [
RichText.configure({
relativePath,
isEmbedded,
component: this,
extensions: [
Mention.configure({
suggestion: MentionSuggestion({
session,
}),
}),
],
}),
FocusTrap,
...extensions,
],
})
}
const createPlainEditor = ({ language, extensions = [] } = {}) => {
return new Editor({
editorProps,
extensions: [
PlainText,
CodeBlockLowlight.configure({
lowlight,
defaultLanguage: language,
exitOnTripleEnter: false,
}),
...extensions,
],
})
}
const serializePlainText = (doc) => {
return doc.textContent
}
export { createRichEditor, createPlainEditor, serializePlainText, loadSyntaxHighlight }