Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/code",
"version": "2.9.3",
"version": "2.9.4",
"keywords": [
"codex editor",
"code",
Expand Down
25 changes: 17 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './index.css';
import { getLineStartPosition } from './utils/string';
import { IconBrackets } from '@codexteam/icons';
import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteConfig, PasteEvent, SanitizerConfig, ToolboxConfig } from '@editorjs/editorjs';
import type { API, BlockTool, BlockToolConstructorOptions, BlockToolData, PasteEvent, HTMLPasteEventDetail, PasteConfig, SanitizerConfig, ToolboxConfig } from '@editorjs/editorjs';

/**
* CodeTool for Editor.js
Expand Down Expand Up @@ -161,14 +161,13 @@ export default class CodeTool implements BlockTool {
* @param event - event with pasted content
*/
public onPaste(event: PasteEvent): void {
const detail = event.detail;
switch (event.type) {
case 'tag': {
const element = (event.detail as HTMLPasteEventDetail).data;

if ('data' in detail) {
const content = detail.data as string;

this.data = {
code: content || '',
};
this.handleHTMLPaste(element);
break;
}
}
}

Expand Down Expand Up @@ -324,4 +323,14 @@ export default class CodeTool implements BlockTool {

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,
};
}
}