/
pagebreakcommand.ts
74 lines (62 loc) · 2.08 KB
/
pagebreakcommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module page-break/pagebreakcommand
*/
import { Command } from 'ckeditor5/src/core.js';
import { findOptimalInsertionRange } from 'ckeditor5/src/widget.js';
import type { DocumentSelection, Element, Model, Schema } from 'ckeditor5/src/engine.js';
/**
* The page break command.
*
* The command is registered by {@link module:page-break/pagebreakediting~PageBreakEditing} as `'pageBreak'`.
*
* To insert a page break at the current selection, execute the command:
*
* editor.execute( 'pageBreak' );
*/
export default class PageBreakCommand extends Command {
/**
* @inheritDoc
*/
public override refresh(): void {
const model = this.editor.model;
const schema = model.schema;
const selection = model.document.selection;
this.isEnabled = isPageBreakAllowedInParent( selection, schema, model );
}
/**
* Executes the command.
*
* @fires execute
*/
public override execute(): void {
const model = this.editor.model;
model.change( writer => {
const pageBreakElement = writer.createElement( 'pageBreak' );
model.insertObject( pageBreakElement, null, null, {
setSelection: 'after'
} );
} );
}
}
/**
* Checks if a page break is allowed by the schema in the optimal insertion parent.
*/
function isPageBreakAllowedInParent( selection: DocumentSelection, schema: Schema, model: Model ): boolean {
const parent = getInsertPageBreakParent( selection, model );
return schema.checkChild( parent, 'pageBreak' );
}
/**
* Returns a node that will be used to insert a page break with `model.insertContent` to check if the page break can be placed there.
*/
function getInsertPageBreakParent( selection: DocumentSelection, model: Model ): Element {
const insertionRange = findOptimalInsertionRange( selection, model );
const parent = insertionRange.start.parent;
if ( parent.isEmpty && !parent.is( 'element', '$root' ) ) {
return parent.parent as Element;
}
return parent as Element;
}