Move split command into its own class

pull/9339/head
Matt Westcott 2022-10-13 12:57:39 +01:00
rodzic afc0c6d0a4
commit e635daf5a3
1 zmienionych plików z 31 dodań i 25 usunięć

Wyświetl plik

@ -213,6 +213,36 @@ class DraftailInsertBlockCommand {
}
}
class DraftailSplitCommand {
/* Definition for a command in the Draftail context menu that splits the block.
* Constructor args:
* split - capability descriptor from the containing block's capabilities definition
*/
constructor(split) {
this.split = split;
this.description = gettext('Split block');
}
icon = '#icon-cut';
type = 'split';
onSelect({ editorState }) {
const result = window.draftail.splitState(
window.draftail.DraftUtils.resetBlockWithType(editorState, 'unstyled'),
);
// Run the split after a timeout to circumvent potential race condition.
setTimeout(() => {
if (result) {
this.split.fn(
result.stateBefore,
result.stateAfter,
result.shouldMoveCommentFn,
);
}
}, 50);
}
}
class BoundDraftailWidget {
constructor(input, options, parentCapabilities) {
this.input = input;
@ -308,31 +338,7 @@ class BoundDraftailWidget {
blockCommands.push({
label: 'Actions',
type: 'custom-actions',
items: [
{
icon: '#icon-cut',
description: gettext('Split block'),
type: 'split',
onSelect: ({ editorState }) => {
const result = window.draftail.splitState(
window.draftail.DraftUtils.resetBlockWithType(
editorState,
'unstyled',
),
);
// Run the split after a timeout to circumvent potential race condition.
setTimeout(() => {
if (result) {
split.fn(
result.stateBefore,
result.stateAfter,
result.shouldMoveCommentFn,
);
}
}, 50);
},
},
],
items: [new DraftailSplitCommand(split)],
});
}
}