Skip to content

Commit 0597816

Browse files
authored
Merge pull request #77 from editor-js/fix-paste-html-embedded-code
fix(paste): replace PasteEvent with HTMLPasteEvent to preserve innerHTML
2 parents 82402cb + cf9f938 commit 0597816

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/code",
3-
"version": "2.9.3",
3+
"version": "2.9.4",
44
"keywords": [
55
"codex editor",
66
"code",

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './index.css';
22
import { getLineStartPosition } from './utils/string';
33
import { IconBrackets } from '@codexteam/icons';
4-
import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteConfig, PasteEvent, SanitizerConfig, ToolboxConfig } from '@editorjs/editorjs';
4+
import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteEvent, HTMLPasteEventDetail, PasteConfig, SanitizerConfig, ToolboxConfig } from '@editorjs/editorjs';
55

66
/**
77
* CodeTool for Editor.js
@@ -161,14 +161,13 @@ export default class CodeTool implements BlockTool {
161161
* @param event - event with pasted content
162162
*/
163163
public onPaste(event: PasteEvent): void {
164-
const detail = event.detail;
164+
switch (event.type) {
165+
case 'tag': {
166+
const element = (event.detail as HTMLPasteEventDetail).data;
165167

166-
if ('data' in detail) {
167-
const content = detail.data as string;
168-
169-
this.data = {
170-
code: content || '',
171-
};
168+
this.handleHTMLPaste(element);
169+
break;
170+
}
172171
}
173172
}
174173

@@ -324,4 +323,14 @@ export default class CodeTool implements BlockTool {
324323

325324
return wrapper;
326325
}
326+
327+
/**
328+
* Extracts the code content from the pasted element's innerHTML and populates the tool's data.
329+
* @param element - pasted HTML element
330+
*/
331+
private handleHTMLPaste(element: HTMLElement): void {
332+
this.data = {
333+
code: element.innerHTML,
334+
};
335+
}
327336
}

0 commit comments

Comments
 (0)