-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (125 loc) · 4.59 KB
/
index.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
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
import {toggleMark} from 'prosemirror-commands';
import './subsup_plugin.css';
function createTextSelection(tr, SelectionClass, from, to) {
const end = to || from;
const contentSize = tr.doc.content.size;
const size = contentSize > 0 ? contentSize - 1 : 1;
return SelectionClass.create(tr.doc, Math.min(from, size), Math.min(end, size));
}
export default function supsubPlugin(context) {
const {eventEmitter, pmState} = context;
let wwEditorEl = null;
let mdEditorEl = null;
let currentEditorEl = null;
eventEmitter.listen('load', (instance) => {
const {mode, wwEditor, mdEditor} = instance;
wwEditorEl = wwEditor.el.firstChild;
mdEditorEl = mdEditor.el.firstChild;
currentEditorEl = mode === 'wysiwyg' ? wwEditorEl : mdEditorEl;
});
eventEmitter.listen('focus', (editType) => {
currentEditorEl = editType === 'wysiwyg' ? wwEditorEl : mdEditorEl;
});
eventEmitter.listen('command', (command) => {
if (['superscript', 'subscript'].includes(command)) currentEditorEl.focus();
});
eventEmitter.listen('caretChange', (editType) => {
const editor = currentEditorEl?.closest('.toastui-editor-defaultUI');
const sel = window.getSelection();
const checkNode = editType === 'markdown'
? sel.anchorNode?.previousSibling?.innerText.replace(/[^\w\s]/gi, '')
: sel.anchorNode?.parentNode?.localName;
const supButton = editor?.querySelector('button.superscript');
const subButton = editor?.querySelector('button.subscript');
if (checkNode && checkNode === 'sup') {
supButton.classList.add('active');
} else if (supButton?.classList.contains('active')) {
supButton.classList.remove('active');
}
if (checkNode && checkNode === 'sub') {
subButton.classList.add('active');
} else if (subButton?.classList.contains('active')) {
subButton.classList.remove('active');
}
});
function toggleMD({openTag, closeTag}, {tr, selection}, dispatch) {
const reSubSup = new RegExp(`^${openTag}.*([sS]*)</su(p|b)>$`, 'm');
function conditionFn(text) { return reSubSup.test(text); }
const syntaxLen = openTag.length;
const endLen = closeTag.length;
const {doc} = tr;
const {from, to} = selection;
const prevPos = Math.max(from - syntaxLen, 1);
const nextPos = Math.min(to + endLen, doc.content.size - 1);
const slice = selection.content();
let textContent = slice.content.textBetween(0, slice.content.size, '\n');
const prevText = doc.textBetween(prevPos, from, '\n');
const nextText = doc.textBetween(to, nextPos, '\n');
textContent = `${prevText}${textContent}${nextText}`;
if (prevText && nextText && conditionFn(textContent)) {
tr.delete(nextPos - endLen, nextPos).delete(prevPos, prevPos + syntaxLen);
} else {
tr.insertText(closeTag, to).insertText(openTag, from);
const newSelection = selection.empty
? createTextSelection(tr, pmState.TextSelection, from + syntaxLen)
: createTextSelection(tr, pmState.TextSelection, from + syntaxLen, to + syntaxLen);
tr.setSelection(newSelection);
}
dispatch(tr);
return true;
}
const subButton = {
name: 'subscript',
tooltip: 'Subscript',
command: 'subscript',
className: 'subscript toastui-editor-toolbar-icons',
};
const superButton = {
name: 'superscript',
tooltip: 'Superscript',
command: 'superscript',
className: 'superscript toastui-editor-toolbar-icons',
};
return {
markdownCommands: {
subscript: (_payload, state, dispatch) => {
const tags = {openTag: '<sub>', closeTag: '</sub>'};
return toggleMD(tags, state, dispatch);
},
superscript: (_payload, state, dispatch) => {
const tags = {openTag: '<sup>', closeTag: '</sup>'};
return toggleMD(tags, state, dispatch);
},
},
wysiwygCommands: {
subscript: (_payload, state, dispatch) => toggleMark(state.schema.marks.sub)(state, dispatch),
superscript: (_payload, state, dispatch) => toggleMark(state.schema.marks.sup)(state, dispatch),
},
toolbarItems: [
{
groupIndex: 0,
itemIndex: 4,
item: subButton,
},
{
groupIndex: 0,
itemIndex: 5,
item: superButton,
},
],
toHTMLRenderers: {
htmlInline: {
sub(node, {entering}) {
return entering
? {type: 'openTag', tagName: 'sub'}
: {type: 'closeTag', tagName: 'sub'};
},
sup(node, {entering}) {
return entering
? {type: 'openTag', tagName: 'sup'}
: {type: 'closeTag', tagName: 'sup'};
},
},
},
};
}