-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTextEditor.svelte
238 lines (217 loc) · 7.15 KB
/
TextEditor.svelte
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!--
// Copyright © 2020, 2021 Anticrm Platform Contributors.
// Copyright © 2021, 2023, 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
import { Analytics } from '@hcengineering/analytics'
import { Markup } from '@hcengineering/core'
import { IntlString, translate } from '@hcengineering/platform'
import { EmptyMarkup, getMarkup, markupToJSON } from '@hcengineering/text'
import { themeStore } from '@hcengineering/ui'
import textEditor from '@hcengineering/text-editor'
import { AnyExtension, Content, Editor, FocusPosition, mergeAttributes } from '@tiptap/core'
import Placeholder from '@tiptap/extension-placeholder'
import { ParseOptions } from '@tiptap/pm/model'
import { EditorView } from '@tiptap/pm/view'
import { createEventDispatcher, onDestroy, onMount } from 'svelte'
import { deleteAttachment } from '../command/deleteAttachment'
import TextEditorToolbar from './TextEditorToolbar.svelte'
import { defaultEditorAttributes } from './editor/editorProps'
import { getEditorKit } from '../../src/kits/editor-kit'
export let content: Markup = EmptyMarkup
export let placeholder: IntlString = textEditor.string.EditorPlaceholder
export let placeholderParams: Record<string, any> = {}
export let extensions: AnyExtension[] = []
export let supportSubmit = true
export let editorAttributes: Record<string, string> = {}
export let boundary: HTMLElement | undefined = undefined
export let autofocus: FocusPosition = false
export let canEmbedFiles = true
export let canEmbedImages = true
export let onPaste: ((view: EditorView, event: ClipboardEvent) => boolean) | undefined = undefined
let element: HTMLElement
let editor: Editor
let placeHolderStr: string = ''
$: ph = translate(placeholder, placeholderParams, $themeStore.language).then((r) => {
if (editor !== undefined && placeHolderStr !== r) {
const placeholderIndex = editor.extensionManager.extensions.findIndex(
(extension) => extension.name === 'placeholder'
)
if (placeholderIndex !== -1) {
editor.extensionManager.extensions[placeholderIndex].options.placeholder = r
editor.view.dispatch(editor.state.tr)
}
}
placeHolderStr = r
})
const dispatch = createEventDispatcher()
export function isEditable (): boolean {
return editor?.isEditable
}
export function setEditable (editable: boolean): void {
if (editor !== undefined) {
editor.setEditable(editable)
}
}
export function submit (): void {
content = getContent()
dispatch('content', content)
}
export function getContent (): Markup {
return getMarkup(editor)
}
export function setContent (newContent: Markup): void {
if (editor !== undefined && content !== newContent) {
content = newContent
editor.commands.setContent(markupToJSON(content))
}
}
export function clear (): void {
if (editor !== undefined) {
content = EmptyMarkup
editor.commands.clearContent(true)
}
}
export function getEditor (): Editor {
return editor
}
export function insertText (text: string): void {
editor?.commands.insertContent(text)
}
export function insertTable (options: { rows?: number, cols?: number, withHeaderRow?: boolean }) {
editor?.commands.insertTable(options)
}
export function insertCodeBlock (pos?: number): void {
editor?.commands.insertContent(
{
type: 'codeBlock',
content: [{ type: 'text', text: ' ' }]
},
{
updateSelection: false
}
)
if (pos !== undefined) {
editor?.commands.focus(pos, { scrollIntoView: false })
}
}
export function insertSeparatorLine (): void {
editor?.commands.setHorizontalRule()
}
export function insertContent (
value: Content,
options?: {
parseOptions?: ParseOptions
updateSelection?: boolean
}
): void {
editor?.commands.insertContent(value, options)
}
export function insertMarkup (markup: Markup): void {
editor?.commands.insertContent(markupToJSON(markup))
}
let needFocus = false
let focused = false
let posFocus: FocusPosition | undefined = undefined
let textToolbarElement: HTMLElement
let imageToolbarElement: HTMLElement
export function focus (position?: FocusPosition): void {
posFocus = position
needFocus = true
}
$: if (editor != null && needFocus) {
if (!focused) {
editor.commands.focus(posFocus)
posFocus = undefined
}
needFocus = false
}
onMount(() => {
void ph.then(async () => {
editor = new Editor({
enableContentCheck: true,
element,
editorProps: {
attributes: mergeAttributes(defaultEditorAttributes, editorAttributes),
handlePaste: onPaste
},
content: markupToJSON(content),
autofocus,
extensions: [
(await getEditorKit()).configure({
mode: 'compact',
file: canEmbedFiles ? {} : false,
image: canEmbedImages
? {
toolbar: {
element: imageToolbarElement,
boundary
}
}
: false,
drawingBoard: false,
textAlign: false,
submit: supportSubmit ? { submit } : false,
toolbar: {
element: textToolbarElement,
boundary
}
}),
Placeholder.configure({ placeholder: placeHolderStr }),
...extensions
],
parseOptions: {
preserveWhitespace: 'full'
},
onTransaction: () => {
// force re-render so `editor.isActive` works as expected
editor = editor
},
onBlur: () => {
focused = false
dispatch('blur')
},
onFocus: () => {
focused = true
dispatch('focus')
},
onUpdate: () => {
content = getContent()
dispatch('value', content)
dispatch('update', content)
},
onContentError: ({ error }) => {
Analytics.handleError(error)
}
})
})
})
onDestroy(() => {
editor?.destroy()
})
/**
* @public
*/
export function removeAttachment (id: string): void {
editor.commands.command(deleteAttachment(id))
}
function handleFocus (): void {
needFocus = true
}
</script>
<TextEditorToolbar bind:toolbar={textToolbarElement} {editor} on:focus={handleFocus} />
{#if canEmbedImages}
<TextEditorToolbar bind:toolbar={imageToolbarElement} kind="image" {editor} on:focus={handleFocus} />
{/if}
<div class="select-text" style="width: 100%;" bind:this={element} on:keydown />