deepseek-1.x-dev/js/ckeditor5_plugins/insert_prompts/src/InsertPromptCommand.js
js/ckeditor5_plugins/insert_prompts/src/InsertPromptCommand.js
import {Command} from "ckeditor5/src/core";
export default class InsertPromptCommand extends Command {
execute(options) {
const editor = this.editor;
const model = editor.model;
const { class: promptClass, library, prompt_id, position } = options;
model.change((writer) => {
const range = writer.createRange(position, position.getShiftedBy(1));
// Check block has class insert-prompt and has data-prompt-id
const blockElement = Array.from(range.getItems()).find((item) => {
if (item.is('element') && item.getAttribute('htmlDivAttributes')) {
const attributes = item.getAttribute('htmlDivAttributes');
return (
attributes &&
attributes.classes.includes('insert-prompt') &&
attributes.attributes['data-prompt-id'] === prompt_id
);
}
return false;
});
if (blockElement) {
// Get current attributes.
const currentAttributes = blockElement.getAttribute('htmlDivAttributes') || {};
const updatedAttributes = {
...currentAttributes,
classes: [promptClass, ...(currentAttributes.classes || [])],
attributes: {
...(currentAttributes.attributes || {}),
'data-library': library,
},
};
// Add attributes.
writer.setAttribute('htmlDivAttributes', updatedAttributes, blockElement);
} else {
console.error('Block element not found at the specified position.');
}
});
}
refresh() {
const {model} = this.editor;
const {selection} = model.document;
const allowedIn = model.schema.findAllowedParent(
selection.getFirstPosition(),
'insertPrompt',
);
this.isEnabled = allowedIn !== null;
}
}
