-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTextEditor.svelte
271 lines (243 loc) · 8.31 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!--
// Copyright © 2020, 2021 Anticrm Platform Contributors.
// Copyright © 2021, 2023 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 { Markup } from '@hcengineering/core'
import { IntlString, translate } from '@hcengineering/platform'
import { EmptyMarkup, getMarkup, markupToJSON } from '@hcengineering/text'
import { themeStore } from '@hcengineering/ui'
import { AnyExtension, Content, Editor, FocusPosition, mergeAttributes } from '@tiptap/core'
import Placeholder from '@tiptap/extension-placeholder'
import { ParseOptions } from '@tiptap/pm/model'
import { createEventDispatcher, onDestroy, onMount } from 'svelte'
import { deleteAttachment } from '../command/deleteAttachment'
import textEditorPlugin from '../plugin'
import { TextFormatCategory } from '../types'
import ImageStyleToolbar from './ImageStyleToolbar.svelte'
import TextEditorStyleToolbar from './TextEditorStyleToolbar.svelte'
import { defaultEditorAttributes } from './editor/editorProps'
import { InlinePopupExtension } from './extension/inlinePopup'
import { InlineStyleToolbarExtension } from './extension/inlineStyleToolbar'
import { SubmitExtension } from './extension/submit'
import { EditorKit } from '../kits/editor-kit'
import { EditorView } from '@tiptap/pm/view'
export let content: Markup = EmptyMarkup
export let placeholder: IntlString = textEditorPlugin.string.EditorPlaceholder
export let extensions: AnyExtension[] = []
export let textFormatCategories: TextFormatCategory[] = []
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, {}, $themeStore.language).then((r) => {
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 (content !== newContent) {
content = newContent
editor.commands.setContent(markupToJSON(content))
}
}
export function clear (): void {
content = EmptyMarkup
editor.commands.clearContent(true)
}
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 insertEmoji (): 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
$: tippyOptions = {
zIndex: 100000,
popperOptions: {
modifiers: [
{
name: 'preventOverflow',
options: {
boundary,
padding: 8,
altAxis: true,
tether: false
}
}
]
}
}
export function focus (position?: FocusPosition): void {
posFocus = position
needFocus = true
}
$: if (editor != null && needFocus) {
if (!focused) {
editor.commands.focus(posFocus)
posFocus = undefined
}
needFocus = false
}
const Handle = SubmitExtension.configure({ submit })
onMount(() => {
void ph.then(() => {
editor = new Editor({
element,
editorProps: {
attributes: mergeAttributes(defaultEditorAttributes, editorAttributes),
handlePaste: onPaste
},
content: markupToJSON(content),
autofocus,
extensions: [
EditorKit.configure({
file: canEmbedFiles ? {} : false,
image: canEmbedImages ? {} : false
}),
...(supportSubmit ? [Handle] : []), // order important
Placeholder.configure({ placeholder: placeHolderStr }),
...extensions,
InlineStyleToolbarExtension.configure({
tippyOptions,
// TODO: Toolbar element is updated on every component update,
// but extensions is created only on mount. This causes issues when
// you're trying to use TextEditor in long-living components that
// get updated, e.g. in QuestionCollectionItemEditor in Surveys
element: textToolbarElement,
isSupported: () => true
}),
InlinePopupExtension.configure({
pluginKey: 'show-image-actions-popup',
// TODO: Toolbar element is updated on every component update,
// but extensions is created only on mount. This causes issues when
// you're trying to use TextEditor in long-living components that
// get updated, e.g. in QuestionCollectionItemEditor in Surveys
element: imageToolbarElement,
tippyOptions: {
...tippyOptions,
appendTo: () => boundary ?? element
},
shouldShow: ({ editor }) => editor.isEditable && editor.isActive('image')
})
],
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)
}
})
})
})
onDestroy(() => {
editor?.destroy()
})
/**
* @public
*/
export function removeAttachment (id: string): void {
editor.commands.command(deleteAttachment(id))
}
function handleFocus (): void {
needFocus = true
}
</script>
<div bind:this={textToolbarElement} class="text-editor-toolbar buttons-group xsmall-gap mb-4">
<TextEditorStyleToolbar textEditor={editor} {textFormatCategories} on:focus={handleFocus} />
</div>
<div bind:this={imageToolbarElement} class="text-editor-toolbar buttons-group xsmall-gap mb-4">
<ImageStyleToolbar textEditor={editor} on:focus={handleFocus} />
</div>
<div class="select-text" style="width: 100%;" bind:this={element} />
<style lang="scss">
.text-editor-toolbar {
margin: -0.5rem -0.25rem 0.5rem;
padding: 0.375rem;
background-color: var(--theme-comp-header-color);
border-radius: 0.5rem;
box-shadow: var(--theme-popup-shadow);
z-index: 1;
}
</style>