-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathplainText.ts
More file actions
100 lines (92 loc) 路 2.65 KB
/
Copy pathplainText.ts
File metadata and controls
100 lines (92 loc) 路 2.65 KB
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
import { Extension, getText, getTextSerializersFromSchema } from '@tiptap/core'
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
import StarterKit from '@tiptap/starter-kit'
import FindAndReplace from '@tiptap/extension-find-and-replace'
import { useGettext } from 'vue3-gettext'
import { EditorActionGroup, useEditorActions } from '../useEditorActions'
import { createPlainTextClipboardExtension } from '../../extensions'
import { ContentTypeStrategy, ExtensionsOptions } from './types'
import { TextEditorState } from '../../types'
export const useStrategyPlainText = (editorState: TextEditorState): ContentTypeStrategy => {
const { $gettext } = useGettext()
const editorContentType = () => {
return 'plainText'
}
const serialize = (doc: ProseMirrorNode): string => {
return getText(doc, {
blockSeparator: '\n',
textSerializers: getTextSerializersFromSchema(doc.type.schema)
})
}
const deserialize = (content: string): Record<string, unknown> => {
if (!content) {
return { type: 'doc', content: [{ type: 'paragraph' }] }
}
const lines = content.split('\n')
return {
type: 'doc',
content: lines.map((line) => {
if (!line) {
return { type: 'paragraph' }
}
return { type: 'paragraph', content: [{ type: 'text', text: line }] }
})
}
}
const extensions = (options?: ExtensionsOptions): Extension[] => {
return [
StarterKit.configure({
undoRedo: options?.yjs ? false : undefined,
blockquote: false,
bold: false,
bulletList: false,
code: false,
codeBlock: false,
gapcursor: false,
heading: false,
horizontalRule: false,
italic: false,
link: false,
listItem: false,
listKeymap: false,
orderedList: false,
strike: false
}),
FindAndReplace,
createPlainTextClipboardExtension()
]
}
const { undo, redo, zoomMenu, print, menuEmoji, menuSearchAndReplace } =
useEditorActions(editorState)
const editorActionGroups = (): EditorActionGroup[] => {
return [
{
id: 'navigation',
title: $gettext('Navigation'),
actions: [undo(), redo(), zoomMenu()]
},
{
id: 'emoji',
title: $gettext('Emoji'),
actions: [menuEmoji()]
},
{
id: 'search',
title: $gettext('Search'),
actions: [menuSearchAndReplace()]
},
{
id: 'export',
title: $gettext('Export'),
actions: [print()]
}
]
}
return {
editorContentType,
serialize,
deserialize,
extensions,
editorActionGroups
}
}