-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsource.ts
More file actions
46 lines (40 loc) · 1.39 KB
/
source.ts
File metadata and controls
46 lines (40 loc) · 1.39 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
import type { Editor } from '@tiptap/core'
import { defineTipTapPlugin } from '../configuration.ts'
const TIPTAP_SOURCE_VIEW_ACTIVE_ATTRIBUTE = 'data-tiptap-source-view-active'
export function getEditorSourceViewActiveStatus(editor: Editor) {
return editor.view.dom.getAttribute(TIPTAP_SOURCE_VIEW_ACTIVE_ATTRIBUTE) === 'true'
}
export function saveEditorSourceViewActiveStatus(editor: Editor, status: boolean) {
editor.view.dom.setAttribute(TIPTAP_SOURCE_VIEW_ACTIVE_ATTRIBUTE, status.toString())
}
export default function () {
return defineTipTapPlugin({
commands: [
{
id: 'source',
label: 'Source',
iconIdentifier: 'source',
position: {
toolbarGroupId: 'developer',
bubbleMenuGroupId: false,
},
status: {
isActive: ({ editor }) => getEditorSourceViewActiveStatus(editor),
},
onExecute: ({ editor }) => {
const isHtmlActive = getEditorSourceViewActiveStatus(editor)
const editorContent = isHtmlActive
? editor.getText()
: `<textarea>${editor.getHTML()}</textarea>`
saveEditorSourceViewActiveStatus(editor, !isHtmlActive)
editor.commands.setContent(editorContent)
},
hooks: {
onEditorMounted: ({ editor }) => {
saveEditorSourceViewActiveStatus(editor, false)
},
},
},
],
})
}