2025-05-26 10:53:12 +03:00
|
|
|
import { CodeBlock, Plugin, ViewDocumentFragment, WidgetToolbarRepository, type ViewNode } from "ckeditor5";
|
|
|
|
|
import CodeBlockLanguageDropdown from "./code_block_language_dropdown";
|
2025-05-26 11:37:26 +03:00
|
|
|
import CopyToClipboardButton from "./copy_to_clipboard_button";
|
2025-05-26 09:17:35 +03:00
|
|
|
|
|
|
|
|
export default class CodeBlockToolbar extends Plugin {
|
|
|
|
|
|
|
|
|
|
static get requires() {
|
2025-05-26 11:37:26 +03:00
|
|
|
return [ WidgetToolbarRepository, CodeBlock, CodeBlockLanguageDropdown, CopyToClipboardButton ] as const;
|
2025-05-26 10:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
2025-05-26 09:17:35 +03:00
|
|
|
afterInit() {
|
|
|
|
|
const editor = this.editor;
|
|
|
|
|
const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
|
|
|
|
|
|
|
|
|
|
widgetToolbarRepository.register("codeblock", {
|
|
|
|
|
items: [
|
2025-05-26 11:37:26 +03:00
|
|
|
"codeBlockDropdown",
|
|
|
|
|
"|",
|
|
|
|
|
"copyToClipboard"
|
2025-05-26 09:17:35 +03:00
|
|
|
],
|
2025-05-26 12:35:30 +03:00
|
|
|
balloonClassName: "ck-toolbar-container codeblock-language-list",
|
2025-05-26 09:17:35 +03:00
|
|
|
getRelatedElement(selection) {
|
|
|
|
|
const selectionPosition = selection.getFirstPosition();
|
|
|
|
|
if (!selectionPosition) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let parent: ViewNode | ViewDocumentFragment | null = selectionPosition.parent;
|
|
|
|
|
while (parent) {
|
|
|
|
|
if (parent.is("element", "pre")) {
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent = parent.parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|