1 | /**
|
2 | * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 | /**
|
6 | * @module table/tablecaption/toggletablecaptioncommand
|
7 | */
|
8 | import { Command } from 'ckeditor5/src/core';
|
9 | import { getCaptionFromTableModelElement, getSelectionAffectedTable } from './utils';
|
10 | /**
|
11 | * The toggle table caption command.
|
12 | *
|
13 | * This command is registered by {@link module:table/tablecaption/tablecaptionediting~TableCaptionEditing} as the
|
14 | * `'toggleTableCaption'` editor command.
|
15 | *
|
16 | * Executing this command:
|
17 | *
|
18 | * * either adds or removes the table caption of a selected table (depending on whether the caption is present or not),
|
19 | * * removes the table caption if the selection is anchored in one.
|
20 | *
|
21 | * ```ts
|
22 | * // Toggle the presence of the caption.
|
23 | * editor.execute( 'toggleTableCaption' );
|
24 | * ```
|
25 | *
|
26 | * **Note**: You can move the selection to the caption right away as it shows up upon executing this command by using
|
27 | * the `focusCaptionOnShow` option:
|
28 | *
|
29 | * ```ts
|
30 | * editor.execute( 'toggleTableCaption', { focusCaptionOnShow: true } );
|
31 | * ```
|
32 | */
|
33 | export default class ToggleTableCaptionCommand extends Command {
|
34 | /**
|
35 | * @inheritDoc
|
36 | */
|
37 | refresh() {
|
38 | const editor = this.editor;
|
39 | const tableElement = getSelectionAffectedTable(editor.model.document.selection);
|
40 | this.isEnabled = !!tableElement;
|
41 | if (!this.isEnabled) {
|
42 | this.value = false;
|
43 | }
|
44 | else {
|
45 | this.value = !!getCaptionFromTableModelElement(tableElement);
|
46 | }
|
47 | }
|
48 | /**
|
49 | * Executes the command.
|
50 | *
|
51 | * ```ts
|
52 | * editor.execute( 'toggleTableCaption' );
|
53 | * ```
|
54 | *
|
55 | * @param options Options for the executed command.
|
56 | * @param options.focusCaptionOnShow When true and the caption shows up, the selection will be moved into it straight away.
|
57 | * @fires execute
|
58 | */
|
59 | execute({ focusCaptionOnShow = false } = {}) {
|
60 | this.editor.model.change(writer => {
|
61 | if (this.value) {
|
62 | this._hideTableCaption(writer);
|
63 | }
|
64 | else {
|
65 | this._showTableCaption(writer, focusCaptionOnShow);
|
66 | }
|
67 | });
|
68 | }
|
69 | /**
|
70 | * Shows the table caption. Also:
|
71 | *
|
72 | * * it attempts to restore the caption content from the `TableCaptionEditing` caption registry,
|
73 | * * it moves the selection to the caption right away, it the `focusCaptionOnShow` option was set.
|
74 | *
|
75 | * @param focusCaptionOnShow Default focus behavior when showing the caption.
|
76 | */
|
77 | _showTableCaption(writer, focusCaptionOnShow) {
|
78 | const model = this.editor.model;
|
79 | const tableElement = getSelectionAffectedTable(model.document.selection);
|
80 | const tableCaptionEditing = this.editor.plugins.get('TableCaptionEditing');
|
81 | const savedCaptionElement = tableCaptionEditing._getSavedCaption(tableElement);
|
82 | // Try restoring the caption from the TableCaptionEditing plugin storage.
|
83 | const newCaptionElement = savedCaptionElement || writer.createElement('caption');
|
84 | model.insertContent(newCaptionElement, tableElement, 'end');
|
85 | if (focusCaptionOnShow) {
|
86 | writer.setSelection(newCaptionElement, 'in');
|
87 | }
|
88 | }
|
89 | /**
|
90 | * Hides the caption of a selected table (or an table caption the selection is anchored to).
|
91 | *
|
92 | * The content of the caption is stored in the `TableCaptionEditing` caption registry to make this
|
93 | * a reversible action.
|
94 | */
|
95 | _hideTableCaption(writer) {
|
96 | const model = this.editor.model;
|
97 | const tableElement = getSelectionAffectedTable(model.document.selection);
|
98 | const tableCaptionEditing = this.editor.plugins.get('TableCaptionEditing');
|
99 | const captionElement = getCaptionFromTableModelElement(tableElement);
|
100 | // Store the caption content so it can be restored quickly if the user changes their mind.
|
101 | tableCaptionEditing._saveCaption(tableElement, captionElement);
|
102 | model.deleteContent(writer.createSelection(captionElement, 'on'));
|
103 | }
|
104 | }
|