@ckeditor/ckeditor5-bookmark 0.0.0-nightly-20241025.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +17 -0
  3. package/README.md +26 -0
  4. package/build/bookmark.js +4 -0
  5. package/ckeditor5-metadata.json +24 -0
  6. package/dist/augmentation.d.ts +28 -0
  7. package/dist/bookmark.d.ts +34 -0
  8. package/dist/bookmarkconfig.d.ts +52 -0
  9. package/dist/bookmarkediting.d.ts +55 -0
  10. package/dist/bookmarkui.d.ts +170 -0
  11. package/dist/index-content.css +4 -0
  12. package/dist/index-editor.css +150 -0
  13. package/dist/index.css +195 -0
  14. package/dist/index.css.map +1 -0
  15. package/dist/index.d.ts +18 -0
  16. package/dist/index.js +1322 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/insertbookmarkcommand.d.ts +42 -0
  19. package/dist/ui/bookmarkactionsview.d.ts +106 -0
  20. package/dist/ui/bookmarkformview.d.ts +122 -0
  21. package/dist/updatebookmarkcommand.d.ts +46 -0
  22. package/dist/utils.d.ts +15 -0
  23. package/lang/contexts.json +13 -0
  24. package/package.json +43 -0
  25. package/src/augmentation.d.ts +24 -0
  26. package/src/augmentation.js +5 -0
  27. package/src/bookmark.d.ts +30 -0
  28. package/src/bookmark.js +36 -0
  29. package/src/bookmarkconfig.d.ts +48 -0
  30. package/src/bookmarkconfig.js +5 -0
  31. package/src/bookmarkediting.d.ts +51 -0
  32. package/src/bookmarkediting.js +212 -0
  33. package/src/bookmarkui.d.ts +166 -0
  34. package/src/bookmarkui.js +583 -0
  35. package/src/index.d.ts +14 -0
  36. package/src/index.js +13 -0
  37. package/src/insertbookmarkcommand.d.ts +38 -0
  38. package/src/insertbookmarkcommand.js +113 -0
  39. package/src/ui/bookmarkactionsview.d.ts +102 -0
  40. package/src/ui/bookmarkactionsview.js +154 -0
  41. package/src/ui/bookmarkformview.d.ts +118 -0
  42. package/src/ui/bookmarkformview.js +203 -0
  43. package/src/updatebookmarkcommand.d.ts +42 -0
  44. package/src/updatebookmarkcommand.js +75 -0
  45. package/src/utils.d.ts +11 -0
  46. package/src/utils.js +19 -0
  47. package/theme/bookmark.css +50 -0
  48. package/theme/bookmarkactions.css +44 -0
  49. package/theme/bookmarkform.css +42 -0
@@ -0,0 +1,212 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 bookmark/bookmarkediting
7
+ */
8
+ import { Plugin, icons } from 'ckeditor5/src/core.js';
9
+ import { toWidget } from 'ckeditor5/src/widget.js';
10
+ import { IconView } from 'ckeditor5/src/ui.js';
11
+ import InsertBookmarkCommand from './insertbookmarkcommand.js';
12
+ import UpdateBookmarkCommand from './updatebookmarkcommand.js';
13
+ import '../theme/bookmark.css';
14
+ const bookmarkIcon = icons.bookmarkInline;
15
+ /**
16
+ * The bookmark editing plugin.
17
+ */
18
+ export default class BookmarkEditing extends Plugin {
19
+ constructor() {
20
+ super(...arguments);
21
+ /**
22
+ * A collection of bookmarks elements in the document.
23
+ */
24
+ this._bookmarkElements = new Map();
25
+ }
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ static get pluginName() {
30
+ return 'BookmarkEditing';
31
+ }
32
+ /**
33
+ * @inheritDoc
34
+ */
35
+ static get isOfficialPlugin() {
36
+ return true;
37
+ }
38
+ /**
39
+ * @inheritDoc
40
+ */
41
+ init() {
42
+ const { editor } = this;
43
+ this._defineSchema();
44
+ this._defineConverters();
45
+ editor.commands.add('insertBookmark', new InsertBookmarkCommand(editor));
46
+ editor.commands.add('updateBookmark', new UpdateBookmarkCommand(editor));
47
+ this.listenTo(editor.model.document, 'change:data', () => {
48
+ this._trackBookmarkElements();
49
+ });
50
+ }
51
+ /**
52
+ * Returns the model element for the given bookmark ID if it exists.
53
+ */
54
+ getElementForBookmarkId(bookmarkId) {
55
+ for (const [element, id] of this._bookmarkElements) {
56
+ if (id == bookmarkId) {
57
+ return element;
58
+ }
59
+ }
60
+ return null;
61
+ }
62
+ /**
63
+ * Defines the schema for the bookmark feature.
64
+ */
65
+ _defineSchema() {
66
+ const schema = this.editor.model.schema;
67
+ schema.register('bookmark', {
68
+ inheritAllFrom: '$inlineObject',
69
+ allowAttributes: 'bookmarkId',
70
+ disallowAttributes: ['linkHref', 'htmlA']
71
+ });
72
+ }
73
+ /**
74
+ * Defines the converters for the bookmark feature.
75
+ */
76
+ _defineConverters() {
77
+ const { editor } = this;
78
+ const { conversion, t } = editor;
79
+ editor.data.htmlProcessor.domConverter.registerInlineObjectMatcher(element => upcastMatcher(element));
80
+ // Register an inline object matcher so that bookmarks <a>s are correctly recognized as inline elements in editing pipeline.
81
+ // This prevents converting spaces around bookmarks to `&nbsp;`s.
82
+ editor.editing.view.domConverter.registerInlineObjectMatcher(element => upcastMatcher(element, false));
83
+ conversion.for('dataDowncast').elementToElement({
84
+ model: {
85
+ name: 'bookmark',
86
+ attributes: ['bookmarkId']
87
+ },
88
+ view: (modelElement, { writer }) => {
89
+ const emptyElement = writer.createEmptyElement('a', {
90
+ 'id': modelElement.getAttribute('bookmarkId')
91
+ });
92
+ // `getFillerOffset` is not needed to set here, because `emptyElement` has already covered it.
93
+ return emptyElement;
94
+ }
95
+ });
96
+ conversion.for('editingDowncast').elementToElement({
97
+ model: {
98
+ name: 'bookmark',
99
+ attributes: ['bookmarkId']
100
+ },
101
+ view: (modelElement, { writer }) => {
102
+ const id = modelElement.getAttribute('bookmarkId');
103
+ const containerElement = writer.createContainerElement('a', {
104
+ id,
105
+ class: 'ck-bookmark'
106
+ }, [this._createBookmarkUIElement(writer)]);
107
+ this._bookmarkElements.set(modelElement, id);
108
+ // `getFillerOffset` is not needed to set here, because `toWidget` has already covered it.
109
+ const labelCreator = () => `${id} ${t('bookmark widget')}`;
110
+ return toWidget(containerElement, writer, { label: labelCreator });
111
+ }
112
+ });
113
+ conversion.for('upcast').add(dispatcher => dispatcher.on('element:a', dataViewModelAnchorInsertion(editor)));
114
+ }
115
+ /**
116
+ * Creates a UI element for the `bookmark` representation in editing view.
117
+ */
118
+ _createBookmarkUIElement(writer) {
119
+ return writer.createUIElement('span', { class: 'ck-bookmark__icon' }, function (domDocument) {
120
+ const domElement = this.toDomElement(domDocument);
121
+ const icon = new IconView();
122
+ icon.set({
123
+ content: bookmarkIcon,
124
+ isColorInherited: false
125
+ });
126
+ icon.render();
127
+ domElement.appendChild(icon.element);
128
+ return domElement;
129
+ });
130
+ }
131
+ /**
132
+ * Tracking the added or removed bookmark elements.
133
+ */
134
+ _trackBookmarkElements() {
135
+ this._bookmarkElements.forEach((id, element) => {
136
+ if (element.root.rootName === '$graveyard') {
137
+ this._bookmarkElements.delete(element);
138
+ }
139
+ });
140
+ }
141
+ }
142
+ /**
143
+ * A helper function to match an `anchor` element which must contain `id` or `name` attribute but without `href` attribute,
144
+ * also when `expectEmpty` is set to `true` but the element is not empty matcher should not match any element.
145
+ *
146
+ * @param element The element to be checked.
147
+ * @param expectEmpty Default set to `true`, when set to `false` matcher expects that `anchor` is not empty;
148
+ * in editing pipeline it's not empty because it contains the `UIElement`.
149
+ */
150
+ function upcastMatcher(element, expectEmpty = true) {
151
+ const isAnchorElement = element.name === 'a';
152
+ if (!isAnchorElement) {
153
+ return null;
154
+ }
155
+ if (expectEmpty && !element.isEmpty) {
156
+ return null;
157
+ }
158
+ const hasIdAttribute = element.hasAttribute('id');
159
+ const hasNameAttribute = element.hasAttribute('name');
160
+ const hasHrefAttribute = element.hasAttribute('href');
161
+ if (hasIdAttribute && !hasHrefAttribute) {
162
+ return { name: true, attributes: ['id'] };
163
+ }
164
+ if (hasNameAttribute && !hasHrefAttribute) {
165
+ return { name: true, attributes: ['name'] };
166
+ }
167
+ return null;
168
+ }
169
+ /**
170
+ * A view-to-model converter that handles converting pointed or wrapped anchors with `id` and/or `name` attributes.
171
+ *
172
+ * @returns Returns a conversion callback.
173
+ */
174
+ function dataViewModelAnchorInsertion(editor) {
175
+ return (evt, data, conversionApi) => {
176
+ const viewItem = data.viewItem;
177
+ const match = upcastMatcher(viewItem, false);
178
+ if (!match || !conversionApi.consumable.test(viewItem, match)) {
179
+ return;
180
+ }
181
+ const enableNonEmptyAnchorConversion = isEnabledNonEmptyAnchorConversion(editor);
182
+ if (!enableNonEmptyAnchorConversion && !viewItem.isEmpty) {
183
+ return;
184
+ }
185
+ const modelWriter = conversionApi.writer;
186
+ const anchorId = viewItem.getAttribute('id');
187
+ const anchorName = viewItem.getAttribute('name');
188
+ const bookmarkId = anchorId || anchorName;
189
+ const bookmark = modelWriter.createElement('bookmark', { bookmarkId });
190
+ if (!conversionApi.safeInsert(bookmark, data.modelCursor)) {
191
+ return;
192
+ }
193
+ conversionApi.consumable.consume(viewItem, match);
194
+ if (anchorId === anchorName) {
195
+ conversionApi.consumable.consume(viewItem, { attributes: ['name'] });
196
+ }
197
+ conversionApi.updateConversionResult(bookmark, data);
198
+ // Convert children uses the result of `bookmark` insertion to convert the `anchor` content
199
+ // after the bookmark element (not inside it).
200
+ const { modelCursor, modelRange } = conversionApi.convertChildren(viewItem, data.modelCursor);
201
+ data.modelCursor = modelCursor;
202
+ data.modelRange = modelWriter.createRange(data.modelRange.start, modelRange.end);
203
+ };
204
+ }
205
+ /**
206
+ * Normalize the bookmark configuration option `enableNonEmptyAnchorConversion`.
207
+ */
208
+ function isEnabledNonEmptyAnchorConversion(editor) {
209
+ const enableNonEmptyAnchorConversion = editor.config.get('bookmark.enableNonEmptyAnchorConversion');
210
+ // When not defined, option `enableNonEmptyAnchorConversion` by default is set to `true`.
211
+ return enableNonEmptyAnchorConversion !== undefined ? enableNonEmptyAnchorConversion : true;
212
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 bookmark/bookmarkui
7
+ */
8
+ import { Plugin } from 'ckeditor5/src/core.js';
9
+ import { ContextualBalloon, type ViewWithCssTransitionDisabler } from 'ckeditor5/src/ui.js';
10
+ import BookmarkFormView from './ui/bookmarkformview.js';
11
+ import BookmarkActionsView from './ui/bookmarkactionsview.js';
12
+ import BookmarkEditing from './bookmarkediting.js';
13
+ /**
14
+ * The UI plugin of the bookmark feature.
15
+ *
16
+ * It registers the `'bookmark'` UI button in the editor's {@link module:ui/componentfactory~ComponentFactory component factory}
17
+ * which inserts the `bookmark` element upon selection.
18
+ */
19
+ export default class BookmarkUI extends Plugin {
20
+ /**
21
+ * The actions view displayed inside of the balloon.
22
+ */
23
+ actionsView: BookmarkActionsView | null;
24
+ /**
25
+ * The form view displayed inside the balloon.
26
+ */
27
+ formView: BookmarkFormView & ViewWithCssTransitionDisabler | null;
28
+ /**
29
+ * The contextual balloon plugin instance.
30
+ */
31
+ private _balloon;
32
+ /**
33
+ * @inheritDoc
34
+ */
35
+ static get requires(): readonly [typeof BookmarkEditing, typeof ContextualBalloon];
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ static get pluginName(): "BookmarkUI";
40
+ /**
41
+ * @inheritDoc
42
+ */
43
+ static get isOfficialPlugin(): true;
44
+ /**
45
+ * @inheritDoc
46
+ */
47
+ init(): void;
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ destroy(): void;
52
+ /**
53
+ * Creates views.
54
+ */
55
+ private _createViews;
56
+ /**
57
+ * Creates the {@link module:bookmark/ui/bookmarkactionsview~BookmarkActionsView} instance.
58
+ */
59
+ private _createActionsView;
60
+ /**
61
+ * Creates the {@link module:bookmark/ui/bookmarkformview~BookmarkFormView} instance.
62
+ */
63
+ private _createFormView;
64
+ /**
65
+ * Creates a toolbar Bookmark button. Clicking this button will show
66
+ * a {@link #_balloon} attached to the selection.
67
+ */
68
+ private _createToolbarBookmarkButton;
69
+ /**
70
+ * Creates a button for `bookmark` command to use either in toolbar or in menu bar.
71
+ */
72
+ private _createButton;
73
+ /**
74
+ * Attaches actions that control whether the balloon panel containing the
75
+ * {@link #formView} should be displayed.
76
+ */
77
+ private _enableBalloonActivators;
78
+ /**
79
+ * Attaches actions that control whether the balloon panel containing the
80
+ * {@link #formView} is visible or not.
81
+ */
82
+ private _enableUserBalloonInteractions;
83
+ /**
84
+ * Updates the button label. If bookmark is selected label is set to 'Update' otherwise
85
+ * it is 'Insert'.
86
+ */
87
+ private _updateFormButtonLabel;
88
+ /**
89
+ * Adds the {@link #actionsView} to the {@link #_balloon}.
90
+ *
91
+ * @internal
92
+ */
93
+ _addActionsView(): void;
94
+ /**
95
+ * Adds the {@link #formView} to the {@link #_balloon}.
96
+ */
97
+ private _addFormView;
98
+ /**
99
+ * Closes the form view. Decides whether the balloon should be hidden completely.
100
+ */
101
+ private _closeFormView;
102
+ /**
103
+ * Removes the {@link #formView} from the {@link #_balloon}.
104
+ */
105
+ private _removeFormView;
106
+ /**
107
+ * Shows the correct UI type. It is either {@link #formView} or {@link #actionsView}.
108
+ */
109
+ private _showUI;
110
+ /**
111
+ * Removes the {@link #formView} from the {@link #_balloon}.
112
+ *
113
+ * See {@link #_addFormView}, {@link #_addActionsView}.
114
+ */
115
+ private _hideUI;
116
+ /**
117
+ * Makes the UI react to the {@link module:ui/editorui/editorui~EditorUI#event:update} event to
118
+ * reposition itself when the editor UI should be refreshed.
119
+ *
120
+ * See: {@link #_hideUI} to learn when the UI stops reacting to the `update` event.
121
+ */
122
+ private _startUpdatingUI;
123
+ /**
124
+ * Returns `true` when {@link #formView} is in the {@link #_balloon}.
125
+ */
126
+ private get _isFormInPanel();
127
+ /**
128
+ * Returns `true` when {@link #actionsView} is in the {@link #_balloon}.
129
+ */
130
+ private get _areActionsInPanel();
131
+ /**
132
+ * Returns `true` when {@link #actionsView} is in the {@link #_balloon} and it is
133
+ * currently visible.
134
+ */
135
+ private get _areActionsVisible();
136
+ /**
137
+ * Returns `true` when {@link #actionsView} or {@link #formView} is in the {@link #_balloon}.
138
+ */
139
+ private get _isUIInPanel();
140
+ /**
141
+ * Returns `true` when {@link #actionsView} or {@link #formView} is in the {@link #_balloon} and it is
142
+ * currently visible.
143
+ */
144
+ private get _isUIVisible();
145
+ /**
146
+ * Returns positioning options for the {@link #_balloon}. They control the way the balloon is attached
147
+ * to the target element or selection.
148
+ */
149
+ private _getBalloonPositionData;
150
+ /**
151
+ * Returns the bookmark {@link module:engine/view/attributeelement~AttributeElement} under
152
+ * the {@link module:engine/view/document~Document editing view's} selection or `null`
153
+ * if there is none.
154
+ */
155
+ private _getSelectedBookmarkElement;
156
+ /**
157
+ * Displays a fake visual selection when the contextual balloon is displayed.
158
+ *
159
+ * This adds a 'bookmark-ui' marker into the document that is rendered as a highlight on selected text fragment.
160
+ */
161
+ private _showFakeVisualSelection;
162
+ /**
163
+ * Hides the fake visual selection created in {@link #_showFakeVisualSelection}.
164
+ */
165
+ private _hideFakeVisualSelection;
166
+ }