forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.js
More file actions
156 lines (134 loc) · 3.01 KB
/
term.js
File metadata and controls
156 lines (134 loc) · 3.01 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
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
/**
* Term plugin for the CodeX Editor
*
* Allows to wrap inline fragment and style it somehow.
*/
class Term {
/**
* @param {object} api - CodeX Editor API
*/
constructor(api) {
this.api = api;
/**
* Toolbar Button
*
* @type {HTMLElement|null}
*/
this.button = null;
/**
* Tag represented the term
*
* @type {string}
*/
this.tag = 'SPAN';
/**
* Class name for term-tag
*
* @type {string}
*/
this.CSS = 'marked';
/**
* CSS classes
*/
this.iconClasses = {
base: 'ce-inline-tool',
term: 'ce-term-tool__icon',
active: 'ce-term-tool__icon--active'
};
}
/**
* Specifies Tool as Inline Toolbar Tool
*
* @return {boolean}
*/
static get isInline() {
return true;
}
/**
* Create button element for Toolbar
*
* @return {HTMLElement}
*/
render() {
this.button = document.createElement('button');
this.button.classList.add(this.iconClasses.base, this.iconClasses.term);
return this.button;
}
/**
* Wrap/Unwrap selected fragment
*
* @param {Range} range - selected fragment
*/
surround(range) {
if (!range) {
return;
}
let termWrapper = this.api.selection.findParentTag(this.tag, this.CSS);
/**
* If start or end of selection is in the highlighted block
*/
if (termWrapper) {
this.unwrap(termWrapper);
} else {
this.wrap(range);
}
}
/**
* Wrap selection with term-tag
*
* @param {Range} range - selected fragment
*/
wrap(range) {
/**
* Create a wrapper for highlighting
*/
let span = document.createElement(this.tag);
span.classList.add(this.CSS);
/**
* SurroundContent throws an error if the Range splits a non-Text node with only one of its boundary points
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Range/surroundContents}
*
* // range.surroundContents(span);
*/
span.appendChild(range.extractContents());
range.insertNode(span);
/**
* Expand (add) selection to highlighted block
*/
this.api.selection.expandToTag(span);
}
/**
* Unwrap term-tag
*
* @param {HTMLElement} termWrapper - term wrapper tag
*/
unwrap(termWrapper) {
/**
* Expand selection to all term-tag
*/
this.api.selection.expandToTag(termWrapper);
let sel = window.getSelection();
let range = sel.getRangeAt(0);
let unwrappedContent = range.extractContents();
/**
* Remove empty term-tag
*/
termWrapper.parentNode.removeChild(termWrapper);
/**
* Insert extracted content
*/
range.insertNode(unwrappedContent);
/**
* Restore selection
*/
sel.removeAllRanges();
sel.addRange(range);
}
/**
* Check and change Term's state for current selection
*/
checkState() {
const termTag = this.api.selection.findParentTag(this.tag, this.CSS);
this.button.classList.toggle(this.iconClasses.active, !!termTag);
}
}