-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathindex.ts
More file actions
336 lines (292 loc) · 8.18 KB
/
index.ts
File metadata and controls
336 lines (292 loc) · 8.18 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import './index.css';
import { getLineStartPosition } from './utils/string';
import { IconBrackets } from '@codexteam/icons';
import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteEvent, HTMLPasteEventDetail, PasteConfig, SanitizerConfig, ToolboxConfig } from '@editorjs/editorjs';
/**
* CodeTool for Editor.js
* @version 2.0.0
* @license MIT
*/
/**
* Data structure for CodeTool's data
*/
export type CodeData = BlockToolData<{
/**
* The code content input by the user
*/
code: string;
}>;
/**
* Configuration options for the CodeTool provided by the user
*/
export interface CodeConfig {
/**
* Placeholder text to display in the input field when it's empty
*/
placeholder: string;
}
/**
* Defines the CSS class names used by CodeTool for styling its elements
*/
interface CodeToolCSS {
/** Block Styling from Editor.js API */
baseClass: string;
/** Input Styling from Editor.js API */
input: string;
/** Wrapper styling */
wrapper: string;
/** Textarea styling */
textarea: string;
}
/**
* Holds references to the DOM elements used by CodeTool
*/
interface CodeToolNodes {
/** Main container or Wrapper for CodeTool */
holder: HTMLDivElement | null;
/** Textarea where user inputs their code */
textarea: HTMLTextAreaElement | null;
}
/**
* Options passed to the constructor of a block tool
*/
export type CodeToolConstructorOptions = BlockToolConstructorOptions<CodeData>;
/**
* Code Tool for the Editor.js allows to include code examples in your articles.
*/
export default class CodeTool implements BlockTool {
/**
* API provided by Editor.js for interacting with the editor's core functionality
*/
private api: API;
/**
* Indicates whether the editor is in read-only mode, preventing modifications
*/
private readOnly: boolean;
/**
* Placeholder text displayed when there is no code content
*/
private placeholder: string;
/**
* Collection of CSS class names used by CodeTool for styling its elements
*/
private CSS: CodeToolCSS;
/**
* DOM nodes related to the CodeTool, including containers and other elements
*/
private nodes: CodeToolNodes;
/**
* Stores the current data (code and other related properties) for the CodeTool
*/
private _data!: CodeData;
/**
* Notify core that read-only mode is supported
* @returns true if read-only mode is supported
*/
public static get isReadOnlySupported(): boolean {
return true;
}
/**
* Allows pressing Enter key to create line breaks inside the CodeTool textarea
* This enables multi-line input within the code editor.
* @returns true if line breaks are allowed in the textarea
*/
public static get enableLineBreaks(): boolean {
return true;
}
/**
* Render plugin`s main Element and fill it with saved data
* @param options - tool constricting options
* @param options.data — previously saved plugin code
* @param options.config - user config for Tool
* @param options.api - Editor.js API
* @param options.readOnly - read only mode flag
*/
constructor({ data, config, api, readOnly }: CodeToolConstructorOptions) {
this.api = api;
this.readOnly = readOnly;
this.placeholder = this.api.i18n.t(config.placeholder as string || CodeTool.DEFAULT_PLACEHOLDER);
this.CSS = {
baseClass: this.api.styles.block,
input: this.api.styles.input,
wrapper: 'ce-code',
textarea: 'ce-code__textarea',
};
this.nodes = {
holder: null,
textarea: null,
};
this.data = {
code: data.code ?? '',
};
this.nodes.holder = this.drawView();
}
/**
* Return Tool's view
* @returns this.nodes.holder - Code's wrapper
*/
public render(): HTMLDivElement {
return this.nodes.holder!;
}
/**
* Extract Tool's data from the view
* @param codeWrapper - CodeTool's wrapper, containing textarea with code
* @returns - saved plugin code
*/
public save(codeWrapper: HTMLDivElement): CodeData {
return {
code: codeWrapper.querySelector('textarea')!.value,
};
}
/**
* onPaste callback fired from Editor`s core
* @param event - event with pasted content
*/
public onPaste(event: PasteEvent): void {
switch (event.type) {
case 'tag': {
const element = (event.detail as HTMLPasteEventDetail).data;
this.handleHTMLPaste(element);
break;
}
}
}
/**
* Returns Tool`s data from private property
* @returns
*/
public get data(): CodeData {
return this._data;
}
/**
* Set Tool`s data to private property and update view
* @param data - saved tool data
*/
public set data(data: CodeData) {
this._data = data;
if (this.nodes.textarea) {
this.nodes.textarea.value = data.code;
}
}
/**
* Get Tool toolbox settings.
* Provides the icon and title to display in the toolbox for the CodeTool.
* @returns An object containing:
* - icon: SVG representation of the Tool's icon
* - title: Title to show in the toolbox
*/
public static get toolbox(): ToolboxConfig {
return {
icon: IconBrackets,
title: 'Code',
};
}
/**
* Default placeholder for CodeTool's textarea
* @returns
*/
public static get DEFAULT_PLACEHOLDER(): string {
return 'Enter a code';
}
/**
* Used by Editor.js paste handling API.
* Provides configuration to handle CODE tag.
* @returns
*/
public static get pasteConfig(): PasteConfig {
return {
tags: ['pre'],
};
}
/**
* Automatic sanitize config
* @returns
*/
public static get sanitize(): SanitizerConfig {
return {
code: true, // Allow HTML tags
};
}
/**
* Handles Tab key pressing (adds/removes indentations)
* @param event - keydown
*/
private tabHandler(event: KeyboardEvent): void {
/**
* Prevent editor.js tab handler
*/
event.stopPropagation();
/**
* Prevent native tab behaviour
*/
event.preventDefault();
const textarea = event.target as HTMLTextAreaElement;
const isShiftPressed = event.shiftKey;
const caretPosition = textarea.selectionStart;
const value = textarea.value;
const indentation = ' ';
let newCaretPosition;
/**
* For Tab pressing, just add an indentation to the caret position
*/
if (!isShiftPressed) {
newCaretPosition = caretPosition + indentation.length;
textarea.value = value.substring(0, caretPosition) + indentation + value.substring(caretPosition);
} else {
/**
* For Shift+Tab pressing, remove an indentation from the start of line
*/
const currentLineStart = getLineStartPosition(value, caretPosition);
const firstLineChars = value.substr(currentLineStart, indentation.length);
if (firstLineChars !== indentation) {
return;
}
/**
* Trim the first two chars from the start of line
*/
textarea.value = value.substring(0, currentLineStart) + value.substring(currentLineStart + indentation.length);
newCaretPosition = caretPosition - indentation.length;
}
/**
* Restore the caret
*/
textarea.setSelectionRange(newCaretPosition, newCaretPosition);
}
/**
* Create Tool's view
* @returns
*/
private drawView(): HTMLDivElement {
const wrapper = document.createElement('div');
const textarea = document.createElement('textarea');
wrapper.classList.add(this.CSS.baseClass, this.CSS.wrapper);
textarea.classList.add(this.CSS.textarea, this.CSS.input);
textarea.value = this.data.code;
textarea.placeholder = this.placeholder;
if (this.readOnly) {
textarea.disabled = true;
}
wrapper.appendChild(textarea);
/**
* Enable keydown handlers
*/
textarea.addEventListener('keydown', (event) => {
switch (event.code) {
case 'Tab':
this.tabHandler(event);
break;
}
});
this.nodes.textarea = textarea;
return wrapper;
}
/**
* Extracts the code content from the pasted element's innerHTML and populates the tool's data.
* @param element - pasted HTML element
*/
private handleHTMLPaste(element: HTMLElement): void {
this.data = {
code: element.innerHTML,
};
}
}