@ebl-vue/editor-full 2.31.34 → 2.31.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.postcssrc.yml +33 -0
  2. package/dist/index.d.ts +1 -2
  3. package/dist/index.mjs +183 -182
  4. package/dist/index.mjs.map +1 -0
  5. package/package.json +1 -1
  6. package/postcss.config.js +15 -0
  7. package/src/components/Editor/Editor.vue +293 -0
  8. package/src/components/index.ts +27 -0
  9. package/src/constants/index.ts +1 -0
  10. package/src/i18n/zh-cn.ts +160 -0
  11. package/src/icons/index.ts +93 -0
  12. package/src/index.ts +21 -0
  13. package/src/installer.ts +21 -0
  14. package/src/plugins/alert/index.ts +455 -0
  15. package/src/plugins/block-alignment/index.ts +117 -0
  16. package/src/plugins/block-alignment/readme.md +1 -0
  17. package/src/plugins/code/LICENSE +21 -0
  18. package/src/plugins/code/index.ts +619 -0
  19. package/src/plugins/code/utils/string.ts +34 -0
  20. package/src/plugins/color-picker/index.ts +132 -0
  21. package/src/plugins/delimiter/index.ts +121 -0
  22. package/src/plugins/drag-drop/index.css +19 -0
  23. package/src/plugins/drag-drop/index.ts +151 -0
  24. package/src/plugins/drag-drop/readme.md +1 -0
  25. package/src/plugins/header/H1.ts +404 -0
  26. package/src/plugins/header/H2.ts +403 -0
  27. package/src/plugins/header/H3.ts +404 -0
  28. package/src/plugins/header/H4.ts +404 -0
  29. package/src/plugins/header/H5.ts +403 -0
  30. package/src/plugins/header/H6.ts +404 -0
  31. package/src/plugins/header/index.ts +15 -0
  32. package/src/plugins/header/types.d.ts +46 -0
  33. package/src/plugins/imageResizeCrop/ImageTune.ts +635 -0
  34. package/src/plugins/imageResizeCrop/index.css +230 -0
  35. package/src/plugins/imageResizeCrop/index.ts +5 -0
  36. package/src/plugins/imageResizeCrop/types.d.ts +23 -0
  37. package/src/plugins/imageTool/index.ts +510 -0
  38. package/src/plugins/imageTool/types/codexteam__ajax.d.ts +89 -0
  39. package/src/plugins/imageTool/types/types.ts +236 -0
  40. package/src/plugins/imageTool/ui.ts +313 -0
  41. package/src/plugins/imageTool/uploader.ts +287 -0
  42. package/src/plugins/imageTool/utils/dom.ts +24 -0
  43. package/src/plugins/imageTool/utils/index.ts +73 -0
  44. package/src/plugins/imageTool/utils/isPromise.ts +10 -0
  45. package/src/plugins/indent/index.ts +695 -0
  46. package/src/plugins/inline-code/index.ts +203 -0
  47. package/src/plugins/list/ListRenderer/ChecklistRenderer.ts +208 -0
  48. package/src/plugins/list/ListRenderer/ListRenderer.ts +73 -0
  49. package/src/plugins/list/ListRenderer/OrderedListRenderer.ts +123 -0
  50. package/src/plugins/list/ListRenderer/UnorderedListRenderer.ts +123 -0
  51. package/src/plugins/list/ListRenderer/index.ts +6 -0
  52. package/src/plugins/list/ListTabulator/index.ts +1179 -0
  53. package/src/plugins/list/index.ts +488 -0
  54. package/src/plugins/list/styles/CssPrefix.ts +4 -0
  55. package/src/plugins/list/types/Elements.ts +14 -0
  56. package/src/plugins/list/types/ItemMeta.ts +40 -0
  57. package/src/plugins/list/types/ListParams.ts +102 -0
  58. package/src/plugins/list/types/ListRenderer.ts +6 -0
  59. package/src/plugins/list/types/OlCounterType.ts +63 -0
  60. package/src/plugins/list/types/index.ts +14 -0
  61. package/src/plugins/list/utils/focusItem.ts +18 -0
  62. package/src/plugins/list/utils/getChildItems.ts +40 -0
  63. package/src/plugins/list/utils/getItemChildWrapper.ts +10 -0
  64. package/src/plugins/list/utils/getItemContentElement.ts +10 -0
  65. package/src/plugins/list/utils/getSiblings.ts +52 -0
  66. package/src/plugins/list/utils/isLastItem.ts +9 -0
  67. package/src/plugins/list/utils/itemHasSublist.ts +10 -0
  68. package/src/plugins/list/utils/normalizeData.ts +83 -0
  69. package/src/plugins/list/utils/removeChildWrapperIfEmpty.ts +31 -0
  70. package/src/plugins/list/utils/renderToolboxInput.ts +113 -0
  71. package/src/plugins/list/utils/stripNumbers.ts +7 -0
  72. package/src/plugins/list/utils/type-guards.ts +8 -0
  73. package/src/plugins/marker/index.ts +199 -0
  74. package/src/plugins/outline/index.ts +62 -0
  75. package/src/plugins/outline/outline.css +52 -0
  76. package/src/plugins/paragraph/index.ts +384 -0
  77. package/src/plugins/paragraph/types/icons.d.ts +4 -0
  78. package/src/plugins/paragraph/utils/makeFragment.ts +17 -0
  79. package/src/plugins/quote/index.ts +203 -0
  80. package/src/plugins/table/index.ts +4 -0
  81. package/src/plugins/table/plugin.ts +255 -0
  82. package/src/plugins/table/table.ts +1202 -0
  83. package/src/plugins/table/toolbox.ts +166 -0
  84. package/src/plugins/table/utils/dom.ts +130 -0
  85. package/src/plugins/table/utils/popover.ts +185 -0
  86. package/src/plugins/table/utils/throttled.ts +22 -0
  87. package/src/plugins/underline/index.ts +214 -0
  88. package/src/plugins/undo/index.ts +526 -0
  89. package/src/plugins/undo/observer.ts +101 -0
  90. package/src/plugins/undo/vanilla-caret-js.ts +102 -0
  91. package/src/style.css +139 -0
  92. package/src/types.ts +3 -0
  93. package/src/utils/AxiosService.ts +87 -0
  94. package/src/utils/index.ts +15 -0
  95. package/src/utils/install.ts +19 -0
  96. package/tsconfig.json +37 -0
  97. package/vite.config.ts +81 -0
@@ -0,0 +1,619 @@
1
+ //https://github.com/yudaapratama/editorjs-shiki
2
+ import '@ebl-vue/editor-render/styles/code.css';
3
+ import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteConfig, PasteEvent, SanitizerConfig, ToolboxConfig } from '@ebl-vue/editorjs';
4
+ import { codeToHtml, bundledLanguagesInfo as languages } from 'shiki'
5
+ import { getLineStartPosition } from './utils/string';
6
+ import { IconCode ,IconCopy} from "../../icons";
7
+
8
+ /**
9
+ * CodeTool for Editor.js
10
+ * @version 1.0.0
11
+ * @license MIT
12
+ */
13
+
14
+ /**
15
+ * Data structure for CodeTool's data
16
+ */
17
+ export type CodeData = BlockToolData<{
18
+ /**
19
+ * The code content input by the user
20
+ */
21
+ code: string;
22
+ lang: string;
23
+ theme: string;
24
+ resizeSize?: number;
25
+ }>;
26
+
27
+ /**
28
+ * Configuration options for the CodeTool provided by the user
29
+ */
30
+ export interface CodeConfig {
31
+ /**
32
+ * Placeholder text to display in the input field when it's empty
33
+ */
34
+ placeholder: string;
35
+ }
36
+
37
+ /**
38
+ * Defines the CSS class names used by CodeTool for styling its elements
39
+ */
40
+ interface CodeToolCSS {
41
+ /** Block Styling from Editor.js API */
42
+ baseClass: string;
43
+ /** Input Styling from Editor.js API */
44
+ input: string;
45
+ /** Wrapper styling */
46
+ wrapper: string;
47
+ /** Textarea styling */
48
+ textarea: string;
49
+ /** Span styling */
50
+ span: string;
51
+ /** Selector Language styling */
52
+ selectorLanguage: string;
53
+ /** Selector Theme styling */
54
+ selectorTheme: string;
55
+ }
56
+
57
+ /**
58
+ * Holds references to the DOM elements used by CodeTool
59
+ */
60
+ interface CodeToolNodes {
61
+ /** Main container or Wrapper for CodeTool */
62
+ holder: HTMLDivElement | null;
63
+ /** Textarea where user inputs their code */
64
+ textarea: HTMLTextAreaElement | null;
65
+ }
66
+
67
+ /**
68
+ * Options passed to the constructor of a block tool
69
+ */
70
+ export type CodeToolConstructorOptions = BlockToolConstructorOptions<CodeData>;
71
+
72
+ /**
73
+ * Code Tool for the Editor.js allows to include code examples in your articles.
74
+ */
75
+ export default class CodeTool implements BlockTool {
76
+ /**
77
+ * API provided by Editor.js for interacting with the editor's core functionality
78
+ */
79
+ private api: API;
80
+ /**
81
+ * Indicates whether the editor is in read-only mode, preventing modifications
82
+ */
83
+ private readOnly: boolean;
84
+ /**
85
+ * Placeholder text displayed when there is no code content
86
+ */
87
+ private placeholder: string;
88
+ /**
89
+ * Collection of CSS class names used by CodeTool for styling its elements
90
+ */
91
+ private CSS: CodeToolCSS;
92
+ /**
93
+ * DOM nodes related to the CodeTool, including containers and other elements
94
+ */
95
+ private nodes: CodeToolNodes;
96
+ /**
97
+ * Stores the current data (code and other related properties) for the CodeTool
98
+ */
99
+ private _data!: CodeData;
100
+ /**
101
+ * Default language for the CodeTool
102
+ */
103
+ private _selectorLanguage: string = '';
104
+ /**
105
+ * Default theme for the CodeTool
106
+ */
107
+ private _selectorTheme: string = '';
108
+ /**
109
+ * Notify core that read-only mode is supported
110
+ * @returns true if read-only mode is supported
111
+ */
112
+ public static get isReadOnlySupported(): boolean {
113
+ return true;
114
+ }
115
+
116
+ /**
117
+ * Allows pressing Enter key to create line breaks inside the CodeTool textarea
118
+ * This enables multi-line input within the code editor.
119
+ * @returns true if line breaks are allowed in the textarea
120
+ */
121
+ public static get enableLineBreaks(): boolean {
122
+ return true;
123
+ }
124
+
125
+ /**
126
+ * Render plugin`s main Element and fill it with saved data
127
+ * @param options - tool constricting options
128
+ * @param options.data — previously saved plugin code
129
+ * @param options.config - user config for Tool
130
+ * @param options.api - Editor.js API
131
+ * @param options.readOnly - read only mode flag
132
+ */
133
+ constructor({ data, config, api, readOnly }: CodeToolConstructorOptions) {
134
+ this.api = api;
135
+ this.readOnly = readOnly;
136
+
137
+ this.placeholder = this.api.i18n.t(config.placeholder as string || CodeTool.DEFAULT_PLACEHOLDER);
138
+
139
+ this._selectorLanguage = data.lang || config.lang || CodeTool.DEFAULT_LANGUAGE
140
+ this._selectorTheme = data.theme || config.theme || CodeTool.DEFAULT_THEME
141
+
142
+ this.CSS = {
143
+ baseClass: this.api.styles.block,
144
+ input: this.api.styles.input,
145
+ wrapper: 'ce-editorjs-x-shiki',
146
+ textarea: 'ce-editorjs-x-shiki__textarea',
147
+ span: 'ce-editorjs-x-shiki__span',
148
+ selectorLanguage: 'ce-editorjs-x-shiki__selector-language',
149
+ selectorTheme: 'ce-editorjs-x-shiki__selector-theme',
150
+ };
151
+
152
+ this.nodes = {
153
+ holder: null,
154
+ textarea: null,
155
+ };
156
+
157
+ this.data = {
158
+ code: data.code ?? '',
159
+ lang: this._selectorLanguage,
160
+ theme: this._selectorTheme,
161
+ resizeSize: data.resizeSize || 0
162
+ };
163
+
164
+ this.nodes.holder = this.drawView()
165
+ }
166
+
167
+ /**
168
+ * Return Tool's view
169
+ * @returns this.nodes.holder - Code's wrapper
170
+ */
171
+ public render(): HTMLDivElement {
172
+ return this.nodes.holder!;
173
+ }
174
+
175
+ /**
176
+ * Extract Tool's data from the view
177
+ * @param codeWrapper - CodeTool's wrapper, containing textarea with code
178
+ * @returns - saved plugin code
179
+ */
180
+ public save(codeWrapper: HTMLDivElement): CodeData {
181
+ return {
182
+ code: codeWrapper.querySelector('textarea')!.value,
183
+ lang: this._selectorLanguage,
184
+ theme: this._selectorTheme,
185
+ resizeSize: this.data.resizeSize || 0
186
+ };
187
+ }
188
+
189
+ /**
190
+ * onPaste callback fired from Editor`s core
191
+ * @param event - event with pasted content
192
+ */
193
+ public onPaste(event: PasteEvent): void {
194
+ const detail = event.detail;
195
+ if ('data' in detail) {
196
+ const content = detail.data as string;
197
+ this.data = {
198
+ code: content || '',
199
+ lang: this._selectorLanguage,
200
+ theme: this._selectorTheme
201
+ };
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Returns Tool`s data from private property
207
+ * @returns
208
+ */
209
+ public get data(): CodeData {
210
+ return this._data;
211
+ }
212
+
213
+ /**
214
+ * Set Tool`s data to private property and update view
215
+ * @param data - saved tool data
216
+ */
217
+ public set data(data: CodeData) {
218
+ this._data = data;
219
+
220
+ if (this.nodes.textarea) {
221
+ this.nodes.textarea.value = data.code;
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Get Tool toolbox settings.
227
+ * Provides the icon and title to display in the toolbox for the CodeTool.
228
+ * @returns An object containing:
229
+ * - icon: SVG representation of the Tool's icon
230
+ * - title: Title to show in the toolbox
231
+ */
232
+ public static get toolbox(): ToolboxConfig {
233
+ return {
234
+ icon: IconCode,
235
+ title: 'Code',
236
+ };
237
+ }
238
+
239
+ /**
240
+ * Default placeholder for CodeTool's textarea
241
+ * @returns
242
+ */
243
+ public static get DEFAULT_PLACEHOLDER(): string {
244
+ return 'Enter your code';
245
+ }
246
+
247
+ /**
248
+ * Default language for CodeTool's textarea
249
+ * @returns
250
+ */
251
+ public static get DEFAULT_LANGUAGE(): string {
252
+ return 'javascript';
253
+ }
254
+
255
+ /**
256
+ * Default theme for CodeTool's textarea
257
+ * @returns
258
+ */
259
+ public static get DEFAULT_THEME(): string {
260
+ return 'vitesse-dark';
261
+ }
262
+
263
+ /**
264
+ * Used by Editor.js paste handling API.
265
+ * Provides configuration to handle CODE tag.
266
+ * @returns
267
+ */
268
+ public static get pasteConfig(): PasteConfig {
269
+ return {
270
+ tags: ['pre'],
271
+ };
272
+ }
273
+
274
+ /**
275
+ * Automatic sanitize config
276
+ * @returns
277
+ */
278
+ public static get sanitize(): SanitizerConfig {
279
+ return {
280
+ code: true, // Allow HTML tags
281
+ };
282
+ }
283
+
284
+ /**
285
+ * Handles Tab key pressing (adds/removes indentations)
286
+ * @param event - keydown
287
+ */
288
+ private tabHandler(event: KeyboardEvent): void {
289
+ /**
290
+ * Prevent editor.js tab handler
291
+ */
292
+ event.stopPropagation();
293
+
294
+ /**
295
+ * Prevent native tab behaviour
296
+ */
297
+ event.preventDefault();
298
+
299
+ const textarea = event.target as HTMLTextAreaElement;
300
+ const isShiftPressed = event.shiftKey;
301
+ const caretPosition = textarea.selectionStart;
302
+ const value = textarea.value;
303
+ const indentation = ' ';
304
+
305
+ let newCaretPosition;
306
+
307
+ /**
308
+ * For Tab pressing, just add an indentation to the caret position
309
+ */
310
+ if (!isShiftPressed) {
311
+ newCaretPosition = caretPosition + indentation.length;
312
+
313
+ textarea.value = value.substring(0, caretPosition) + indentation + value.substring(caretPosition);
314
+ } else {
315
+ /**
316
+ * For Shift+Tab pressing, remove an indentation from the start of line
317
+ */
318
+ const currentLineStart = getLineStartPosition(value, caretPosition);
319
+ const firstLineChars = value.substr(currentLineStart, indentation.length);
320
+
321
+ if (firstLineChars !== indentation) {
322
+ return;
323
+ }
324
+
325
+ /**
326
+ * Trim the first two chars from the start of line
327
+ */
328
+ textarea.value = value.substring(0, currentLineStart) + value.substring(currentLineStart + indentation.length);
329
+ newCaretPosition = caretPosition - indentation.length;
330
+ }
331
+
332
+ /**
333
+ * Restore the caret
334
+ */
335
+ textarea.setSelectionRange(newCaretPosition, newCaretPosition);
336
+ }
337
+
338
+ /**
339
+ * Create Tool's view
340
+ * @returns
341
+ */
342
+ private drawView(): HTMLDivElement {
343
+ const wrapper = document.createElement('div');
344
+ const wrapperHolder = document.createElement('div');
345
+ const wrapperSelectorHolder = document.createElement('div');
346
+ const wrapperLang = document.createElement('div');
347
+ const wrapperCopy = document.createElement('div');
348
+ const wrapperCopyTip = document.createElement('div');
349
+
350
+
351
+ const selectorLanguage = document.createElement('select');
352
+ const selectorTheme = document.createElement('select');
353
+ const span = document.createElement('span');
354
+ const textarea = document.createElement('textarea');
355
+ const dragHandle = document.createElement('div');
356
+ dragHandle.classList.add('ce-editorjs-x-shiki__code__dragHandler');
357
+ wrapper.classList.add(this.CSS.baseClass, this.CSS.wrapper);
358
+ if(this.readOnly){
359
+ wrapper.classList.add('read-only');
360
+ }else{
361
+ wrapper.classList.add('editable');
362
+ }
363
+ wrapperHolder.classList.add('ce-editorjs-x-shiki__code');
364
+ wrapperSelectorHolder.classList.add('ce-editorjs-x-shiki__selector');
365
+ wrapperLang.classList.add('ce-editorjs-x-shiki__lang');
366
+ wrapperCopy.classList.add('ce-editorjs-x-shiki__copy');
367
+ wrapperCopyTip.classList.add('ce-editorjs-x-shiki__copy_tip');
368
+
369
+ selectorLanguage.classList.add(this.CSS.selectorLanguage);
370
+ //selectorTheme.classList.add(this.CSS.selectorTheme);
371
+ textarea.classList.add(this.CSS.textarea, this.CSS.input);
372
+
373
+ wrapperLang.innerHTML = this.data.lang
374
+ wrapperCopy.innerHTML = IconCopy;
375
+ wrapperCopyTip.innerText = this.api.i18n.t('Copied');
376
+
377
+
378
+
379
+ languages.forEach((lang) => {
380
+ const option = document.createElement('option');
381
+ option.value = lang.id;
382
+ option.text = lang.name;
383
+ selectorLanguage.appendChild(option);
384
+ });
385
+ selectorLanguage.value = this.data.lang
386
+
387
+ // themes.forEach((theme) => {
388
+ // const option = document.createElement('option');
389
+ // option.value = theme.id;
390
+ // option.text = theme.displayName;
391
+ // selectorTheme.appendChild(option);
392
+ // });
393
+ // selectorTheme.value = this.data.theme
394
+
395
+ textarea.value = this.data.code;
396
+ textarea.placeholder = this.placeholder;
397
+ textarea.spellcheck = false;
398
+ textarea.autocomplete = "off"
399
+ textarea.autocapitalize = "off"
400
+
401
+ if (this.readOnly) {
402
+ textarea.disabled = true;
403
+ }
404
+
405
+ wrapperHolder.appendChild(span);
406
+ wrapperHolder.appendChild(textarea);
407
+ wrapper.appendChild(dragHandle);
408
+
409
+
410
+ if(this.data.resizeSize&&this.data.resizeSize>0){
411
+ wrapperHolder.style.height = this.data.resizeSize + 'px';
412
+ }
413
+
414
+ if (!this.readOnly) {
415
+ wrapperSelectorHolder.appendChild(selectorLanguage);
416
+ //wrapperSelectorHolder.appendChild(selectorTheme);
417
+
418
+ } else {
419
+ wrapperSelectorHolder.appendChild(wrapperLang);
420
+
421
+ }
422
+ wrapperCopy.appendChild(wrapperCopyTip);
423
+
424
+ wrapperSelectorHolder.appendChild(wrapperCopy);
425
+ wrapper.appendChild(wrapperSelectorHolder);
426
+
427
+ wrapper.appendChild(wrapperHolder);
428
+ //wrapper.appendChild(wrapperLang);
429
+
430
+ this.runShiki().then(({ html, preStyle }) => {
431
+
432
+ span.innerHTML = html
433
+ wrapper?.setAttribute('style', preStyle)
434
+ selectorLanguage.setAttribute('style', preStyle)
435
+ selectorTheme.setAttribute('style', preStyle)
436
+ })
437
+
438
+ selectorLanguage.addEventListener('change', (event: Event) => {
439
+ const lang = (event.target as HTMLSelectElement).value
440
+ this._selectorLanguage = lang
441
+ this.runShiki().then(({ html, preStyle }) => {
442
+ span.innerHTML = html
443
+ wrapperLang.innerHTML = lang
444
+ })
445
+ })
446
+
447
+ selectorTheme.addEventListener('change', (event: Event) => {
448
+ const theme = (event.target as HTMLSelectElement).value
449
+ this._selectorTheme = theme
450
+ this.runShiki().then(({ html, preStyle }) => {
451
+ span.innerHTML = html
452
+ wrapper?.setAttribute('style', preStyle)
453
+ selectorLanguage.setAttribute('style', preStyle)
454
+ selectorTheme.setAttribute('style', preStyle)
455
+ })
456
+ })
457
+
458
+ textarea.addEventListener('input', () => {
459
+ this.data.code = textarea.value
460
+ this.runShiki().then(({ html, preStyle }) => {
461
+ span.innerHTML = html
462
+ wrapper?.setAttribute('style', preStyle)
463
+ selectorLanguage.setAttribute('style', preStyle)
464
+ selectorTheme.setAttribute('style', preStyle)
465
+ })
466
+ })
467
+
468
+ /**
469
+ * Enable keydown handlers
470
+ */
471
+ textarea.addEventListener('keydown', (event) => {
472
+ switch (event.code) {
473
+ case 'Tab':
474
+ this.tabHandler(event);
475
+ this.data.code = textarea.value
476
+ this.runShiki().then(({ html, preStyle }) => {
477
+ span.innerHTML = html
478
+ })
479
+ break;
480
+ }
481
+ });
482
+ textarea.addEventListener('scroll', () => {
483
+ //span.scrollTop = textarea.scrollTop
484
+ span.style=`display:block;transform: translateY(-${textarea.scrollTop}px)`
485
+ })
486
+ wrapperCopy.addEventListener('click', (event: MouseEvent) => {
487
+ this.copyCode(this.data.code, event);
488
+ });
489
+ //调整窗口大小
490
+ dragHandle.addEventListener('mousedown', (event: MouseEvent) => {
491
+ this.resizeClick(wrapperHolder, event);
492
+ });
493
+ this.nodes.textarea = textarea;
494
+
495
+ return wrapper;
496
+ }
497
+ private resizeClick(codeElement: HTMLElement, event: MouseEvent) {
498
+
499
+ let startY = event.clientY;
500
+ let startHeight = parseInt(window.getComputedStyle(codeElement).height, 10);
501
+ const mouseMoveHandler = (e: MouseEvent) => {
502
+ const dY = e.clientY - startY;
503
+ let newHeight = startHeight + dY;
504
+
505
+
506
+ if (newHeight < 100) {
507
+ newHeight = 100;
508
+ }
509
+ codeElement.style.height = newHeight + 'px';
510
+ };
511
+ const mouseUpHandler = () => {
512
+ const blockHeight = parseInt(
513
+ window.getComputedStyle(codeElement).height,
514
+ 10,
515
+ );
516
+ this.data.resizeSize = blockHeight;
517
+ document.removeEventListener('mousemove', mouseMoveHandler);
518
+ document.removeEventListener('mouseup', mouseUpHandler);
519
+ };
520
+
521
+ document.addEventListener('mousemove', mouseMoveHandler);
522
+ document.addEventListener('mouseup', mouseUpHandler);
523
+ }
524
+ private async runShiki(): Promise<{ html: string, preStyle: string }> {
525
+ let preStyle = ''
526
+
527
+ const html = await codeToHtml(this.data.code, {
528
+ lang: this._selectorLanguage,
529
+ theme: this._selectorTheme,
530
+
531
+ transformers: [
532
+ {
533
+ preprocess(code) {
534
+ // if (code.endsWith('\n'))
535
+ return `${code}\n`
536
+ },
537
+ pre(node) {
538
+ this.addClassToHast(node, 'ce-editorjs-x-shiki__span')
539
+ preStyle = node.properties?.style as string || ''
540
+ },
541
+ line(node, line) {
542
+ node.properties = node.properties || {};
543
+ node.properties['data-line'] = line;
544
+
545
+ }
546
+
547
+ }
548
+ ]
549
+ })
550
+
551
+ return {
552
+ html,
553
+ preStyle
554
+ }
555
+
556
+ }
557
+ private showCopyTip(msg: string,event: MouseEvent) {
558
+ const tip = this.api.i18n.t(msg);
559
+ if (event.target) {
560
+ const parentEle = (event.target as HTMLElement).parentElement;
561
+ if (parentEle) {
562
+ const tooltip = parentEle.lastChild as HTMLElement;
563
+ if (tooltip) {
564
+ tooltip.innerHTML = tip;
565
+ tooltip.classList.add('visible');
566
+ setTimeout(() => {
567
+ tooltip.classList.remove('visible');
568
+
569
+ }, 2000);
570
+ }
571
+ }
572
+ }
573
+ }
574
+ private copyCode(code: string, event: MouseEvent) {
575
+ if (this.data.code) {
576
+ if (navigator.clipboard && window.isSecureContext) {
577
+ try {
578
+ navigator.clipboard.writeText(code).then(() => {
579
+ this.showCopyTip('Copied',event);
580
+ }).catch((err) => {
581
+ console.error(err);
582
+ this.showCopyTip('Unable to copy',event);
583
+ });
584
+
585
+ } catch (err) {
586
+ this.showCopyTip('Unable to copy',event);
587
+ }
588
+ } else {
589
+ // 传统方法:使用 textarea 和 execCommand
590
+
591
+ const textArea = document.createElement('textarea');
592
+ textArea.value = code;
593
+
594
+ // 避免滚动到底部
595
+ textArea.style.top = '0';
596
+ textArea.style.left = '0';
597
+ textArea.style.position = 'fixed';
598
+ textArea.style.opacity = '0';
599
+ textArea.style.pointerEvents = 'none';
600
+ textArea.style.zIndex = '-1000';
601
+
602
+ document.body.appendChild(textArea);
603
+ textArea.focus();
604
+ textArea.select();
605
+ try {
606
+ document.execCommand('copy');
607
+ document.body.removeChild(textArea);
608
+ this.showCopyTip('Copied',event);
609
+ } catch (err) {
610
+ this.showCopyTip('Unable to copy',event);
611
+ document.body.removeChild(textArea);
612
+
613
+ }
614
+
615
+ }
616
+ }
617
+
618
+ }
619
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Return the position of line starting from passed point
3
+ *
4
+ * ┌───────────────┐
5
+ * │1234\n │
6
+ * │2eda | dadd\n │ <-- returns 5
7
+ * └───────────────┘
8
+ * @param string - string to process
9
+ * @param position - search starting position
10
+ * @returns
11
+ */
12
+ export function getLineStartPosition(string: string, position: number): number {
13
+ const charLength = 1;
14
+ let char = '';
15
+
16
+ /**
17
+ * Iterate through all the chars before the position till the
18
+ * - end of line (\n)
19
+ * - or start of string (position === 0)
20
+ */
21
+ while (char !== '\n' && position > 0) {
22
+ position = position - charLength;
23
+ char = string.substr(position, charLength);
24
+ }
25
+
26
+ /**
27
+ * Do not count the linebreak symbol because it is related to the previous line
28
+ */
29
+ if (char === '\n') {
30
+ position += 1;
31
+ }
32
+
33
+ return position;
34
+ }