@tiptap/extension-invisible-characters 3.30.1 → 3.30.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,52 +1,21 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- HardBreakNode: () => HardBreakNode,
24
- InvisibleCharacter: () => InvisibleCharacter,
25
- InvisibleCharacters: () => InvisibleCharacters,
26
- InvisibleNode: () => InvisibleNode,
27
- ParagraphNode: () => ParagraphNode,
28
- SpaceCharacter: () => SpaceCharacter,
29
- default: () => index_default
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
30
4
  });
31
- module.exports = __toCommonJS(index_exports);
32
-
33
- // src/invisible-characters.ts
34
- var import_core = require("@tiptap/core");
35
-
36
- // src/plugin/InvisibleCharactersPlugin.ts
37
- var import_state = require("@tiptap/pm/state");
38
- var import_view = require("@tiptap/pm/view");
39
-
40
- // src/plugin/reducers.ts
41
- var stateReducer = (state, visible) => {
42
- if (visible === void 0) {
43
- return state;
44
- }
45
- return { ...state, visible };
5
+ let _tiptap_core = require("@tiptap/core");
6
+ let _tiptap_pm_state = require("@tiptap/pm/state");
7
+ let _tiptap_pm_view = require("@tiptap/pm/view");
8
+ //#region src/plugin/reducers.ts
9
+ const stateReducer = (state, visible) => {
10
+ if (visible === void 0) return state;
11
+ return {
12
+ ...state,
13
+ visible
14
+ };
46
15
  };
47
-
48
- // src/plugin/style.ts
49
- var style = `.tiptap-invisible-character {
16
+ //#endregion
17
+ //#region src/plugin/style.ts
18
+ const style = `.tiptap-invisible-character {
50
19
  height: 0;
51
20
  padding: 0;
52
21
  pointer-events: none;
@@ -65,15 +34,15 @@ var style = `.tiptap-invisible-character {
65
34
  }
66
35
 
67
36
  .tiptap-invisible-character--space::before {
68
- content: '\xB7'
37
+ content: '·'
69
38
  }
70
39
 
71
40
  .tiptap-invisible-character--break::before {
72
- content: '\xAC'
41
+ content: '¬'
73
42
  }
74
43
 
75
44
  .tiptap-invisible-character--paragraph::before {
76
- content: '\xB6'
45
+ content: ''
77
46
  }
78
47
 
79
48
  .tiptap-invisible-character + img.ProseMirror-separator {
@@ -87,273 +56,239 @@ var style = `.tiptap-invisible-character {
87
56
  display: none;
88
57
  }
89
58
  `;
90
-
91
- // src/plugin/utils/create-style-tag.ts
92
- var createStyleTag = (style2, nonce) => {
93
- const tiptapStyleTag = document.querySelector("style[data-tiptap-extension-invisible-characters-style]");
94
- if (tiptapStyleTag !== null) {
95
- return tiptapStyleTag;
96
- }
97
- const styleNode = document.createElement("style");
98
- if (nonce) {
99
- styleNode.setAttribute("nonce", nonce);
100
- }
101
- styleNode.setAttribute("data-tiptap-extension-invisible-characters-style", "");
102
- styleNode.innerHTML = style2;
103
- document.getElementsByTagName("head")[0].appendChild(styleNode);
104
- return styleNode;
59
+ //#endregion
60
+ //#region src/plugin/utils/create-style-tag.ts
61
+ const createStyleTag = (style, nonce) => {
62
+ const tiptapStyleTag = document.querySelector("style[data-tiptap-extension-invisible-characters-style]");
63
+ if (tiptapStyleTag !== null) return tiptapStyleTag;
64
+ const styleNode = document.createElement("style");
65
+ if (nonce) styleNode.setAttribute("nonce", nonce);
66
+ styleNode.setAttribute("data-tiptap-extension-invisible-characters-style", "");
67
+ styleNode.innerHTML = style;
68
+ document.getElementsByTagName("head")[0].appendChild(styleNode);
69
+ return styleNode;
105
70
  };
106
-
107
- // src/plugin/utils/get-updated-ranges.ts
108
- var getUpdatedRanges = ({ mapping }) => {
109
- const ranges = [];
110
- mapping.maps.forEach((stepMap, i) => {
111
- stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
112
- ranges.push([mapping.slice(i + 1).map(newStart), mapping.slice(i + 1).map(newEnd)]);
113
- });
114
- });
115
- return ranges;
71
+ //#endregion
72
+ //#region src/plugin/utils/get-updated-ranges.ts
73
+ const getUpdatedRanges = ({ mapping }) => {
74
+ const ranges = [];
75
+ mapping.maps.forEach((stepMap, i) => {
76
+ stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
77
+ ranges.push([mapping.slice(i + 1).map(newStart), mapping.slice(i + 1).map(newEnd)]);
78
+ });
79
+ });
80
+ return ranges;
116
81
  };
117
-
118
- // src/plugin/InvisibleCharactersPlugin.ts
119
- var InvisibleCharactersPluginKey = new import_state.PluginKey("invisibleCharacters");
120
- var InvisibleCharactersPlugin = (state, options) => {
121
- const key = InvisibleCharactersPluginKey;
122
- const emptyDecorationSet = import_view.DecorationSet.create(state.doc, []);
123
- const addDecorationsBetweenPositions = (from, to, editorState, decorations) => {
124
- return options.builders.sort((builderA, builderB) => {
125
- if (builderA.priority > builderB.priority) {
126
- return 1;
127
- }
128
- return -1;
129
- }).reduce((newDecos, builder) => {
130
- return builder.createDecoration(from, to, editorState.doc, newDecos);
131
- }, decorations);
132
- };
133
- return new import_state.Plugin({
134
- key,
135
- state: {
136
- init: () => {
137
- const { $from, $to } = new import_state.AllSelection(state.doc);
138
- if (options.injectCSS && document) {
139
- createStyleTag(style, options.injectNonce);
140
- }
141
- return {
142
- visible: options.visible,
143
- decorations: addDecorationsBetweenPositions(
144
- $from.pos,
145
- $to.pos,
146
- state,
147
- import_view.DecorationSet.empty
148
- )
149
- };
150
- },
151
- apply: (tr, pluginState, _, currentState) => {
152
- const newPluginState = stateReducer(
153
- pluginState,
154
- tr.getMeta("setInvisibleCharactersVisible")
155
- );
156
- const decorations = getUpdatedRanges(tr).reduce(
157
- (nextDecorations, [from, to]) => {
158
- return addDecorationsBetweenPositions(from, to, currentState, nextDecorations);
159
- },
160
- newPluginState.decorations.map(tr.mapping, tr.doc)
161
- );
162
- return {
163
- ...newPluginState,
164
- decorations
165
- };
166
- }
167
- },
168
- props: {
169
- decorations(editorState) {
170
- const pluginState = this.getState(editorState);
171
- const visible = pluginState == null ? void 0 : pluginState.visible;
172
- const decorations = pluginState == null ? void 0 : pluginState.decorations;
173
- return visible ? decorations : emptyDecorationSet;
174
- }
175
- }
176
- });
82
+ //#endregion
83
+ //#region src/plugin/InvisibleCharactersPlugin.ts
84
+ const InvisibleCharactersPluginKey = new _tiptap_pm_state.PluginKey("invisibleCharacters");
85
+ const InvisibleCharactersPlugin = (state, options) => {
86
+ const key = InvisibleCharactersPluginKey;
87
+ const emptyDecorationSet = _tiptap_pm_view.DecorationSet.create(state.doc, []);
88
+ const addDecorationsBetweenPositions = (from, to, editorState, decorations) => {
89
+ return options.builders.sort((builderA, builderB) => {
90
+ if (builderA.priority > builderB.priority) return 1;
91
+ return -1;
92
+ }).reduce((newDecos, builder) => {
93
+ return builder.createDecoration(from, to, editorState.doc, newDecos);
94
+ }, decorations);
95
+ };
96
+ return new _tiptap_pm_state.Plugin({
97
+ key,
98
+ state: {
99
+ init: () => {
100
+ const { $from, $to } = new _tiptap_pm_state.AllSelection(state.doc);
101
+ if (options.injectCSS && document) createStyleTag(style, options.injectNonce);
102
+ return {
103
+ visible: options.visible,
104
+ decorations: addDecorationsBetweenPositions($from.pos, $to.pos, state, _tiptap_pm_view.DecorationSet.empty)
105
+ };
106
+ },
107
+ apply: (tr, pluginState, _, currentState) => {
108
+ const newPluginState = stateReducer(pluginState, tr.getMeta("setInvisibleCharactersVisible"));
109
+ const decorations = getUpdatedRanges(tr).reduce((nextDecorations, [from, to]) => {
110
+ return addDecorationsBetweenPositions(from, to, currentState, nextDecorations);
111
+ }, newPluginState.decorations.map(tr.mapping, tr.doc));
112
+ return {
113
+ ...newPluginState,
114
+ decorations
115
+ };
116
+ }
117
+ },
118
+ props: { decorations(editorState) {
119
+ const pluginState = this.getState(editorState);
120
+ const visible = pluginState === null || pluginState === void 0 ? void 0 : pluginState.visible;
121
+ const decorations = pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
122
+ return visible ? decorations : emptyDecorationSet;
123
+ } }
124
+ });
177
125
  };
178
-
179
- // src/plugin/utils/create-decoration-widget.ts
180
- var import_view2 = require("@tiptap/pm/view");
181
- var createDecorationWidget = (pos, type, content) => {
182
- const createElement = () => {
183
- const newElement = document.createElement("span");
184
- newElement.classList.add("tiptap-invisible-character");
185
- newElement.classList.add(`tiptap-invisible-character--${type}`);
186
- if (content) {
187
- newElement.textContent = content;
188
- }
189
- return newElement;
190
- };
191
- return import_view2.Decoration.widget(pos, createElement, {
192
- key: type,
193
- marks: [],
194
- side: 1e3
195
- });
126
+ //#endregion
127
+ //#region src/plugin/utils/create-decoration-widget.ts
128
+ const createDecorationWidget = (pos, type, content) => {
129
+ const createElement = () => {
130
+ const newElement = document.createElement("span");
131
+ newElement.classList.add("tiptap-invisible-character");
132
+ newElement.classList.add(`tiptap-invisible-character--${type}`);
133
+ if (content) newElement.textContent = content;
134
+ return newElement;
135
+ };
136
+ return _tiptap_pm_view.Decoration.widget(pos, createElement, {
137
+ key: type,
138
+ marks: [],
139
+ side: 1e3
140
+ });
196
141
  };
197
-
198
- // src/plugin/InvisibleNode.ts
199
- var defaultPosition = (node, pos) => pos + node.nodeSize - 1;
142
+ //#endregion
143
+ //#region src/plugin/InvisibleNode.ts
144
+ /** Default position for nodes is always at the end of the node */
145
+ const defaultPosition = (node, pos) => pos + node.nodeSize - 1;
200
146
  var InvisibleNode = class {
201
- constructor(options) {
202
- this.predicate = options.predicate;
203
- this.type = options.type;
204
- this.position = options.position || defaultPosition;
205
- this.content = options.content;
206
- this.priority = options.priority || 100;
207
- }
208
- createDecoration(from, to, doc, decorations) {
209
- let newDecorations = decorations;
210
- doc.nodesBetween(from, to, (node, pos) => {
211
- if (this.test(node)) {
212
- const decorationPosition = this.position(node, pos);
213
- const oldDecorations = newDecorations.find(
214
- decorationPosition,
215
- decorationPosition,
216
- (spec) => spec.key === this.type
217
- );
218
- newDecorations = newDecorations.remove(oldDecorations).add(doc, [createDecorationWidget(decorationPosition, this.type, this.content)]);
219
- }
220
- });
221
- return newDecorations;
222
- }
223
- test(value) {
224
- return this.predicate(value);
225
- }
147
+ constructor(options) {
148
+ this.predicate = options.predicate;
149
+ this.type = options.type;
150
+ this.position = options.position || defaultPosition;
151
+ this.content = options.content;
152
+ this.priority = options.priority || 100;
153
+ }
154
+ createDecoration(from, to, doc, decorations) {
155
+ let newDecorations = decorations;
156
+ doc.nodesBetween(from, to, (node, pos) => {
157
+ if (this.test(node)) {
158
+ const decorationPosition = this.position(node, pos);
159
+ const oldDecorations = newDecorations.find(decorationPosition, decorationPosition, (spec) => spec.key === this.type);
160
+ newDecorations = newDecorations.remove(oldDecorations).add(doc, [createDecorationWidget(decorationPosition, this.type, this.content)]);
161
+ }
162
+ });
163
+ return newDecorations;
164
+ }
165
+ test(value) {
166
+ return this.predicate(value);
167
+ }
226
168
  };
227
-
228
- // src/plugin/invisible-characters/hardBreak.ts
169
+ //#endregion
170
+ //#region src/plugin/invisible-characters/hardBreak.ts
229
171
  var HardBreakNode = class extends InvisibleNode {
230
- constructor() {
231
- super({
232
- type: "break",
233
- predicate: (node) => node.type === node.type.schema.nodes.hardBreak
234
- });
235
- }
172
+ constructor() {
173
+ super({
174
+ type: "break",
175
+ predicate: (node) => node.type === node.type.schema.nodes.hardBreak
176
+ });
177
+ }
236
178
  };
237
-
238
- // src/plugin/invisible-characters/paragraph.ts
179
+ //#endregion
180
+ //#region src/plugin/invisible-characters/paragraph.ts
239
181
  var ParagraphNode = class extends InvisibleNode {
240
- constructor() {
241
- super({
242
- type: "paragraph",
243
- predicate: (node) => node.type === node.type.schema.nodes.paragraph
244
- });
245
- }
182
+ constructor() {
183
+ super({
184
+ type: "paragraph",
185
+ predicate: (node) => node.type === node.type.schema.nodes.paragraph
186
+ });
187
+ }
246
188
  };
247
-
248
- // src/plugin/utils/text-between.ts
249
- var textBetween = (from, to, doc) => {
250
- const positions = [];
251
- doc.nodesBetween(from, to, (node, pos) => {
252
- var _a;
253
- if (node.isText) {
254
- const offset = Math.max(from, pos) - pos;
255
- positions.push({
256
- pos: pos + offset,
257
- text: ((_a = node.text) == null ? void 0 : _a.slice(offset, to - pos)) || ""
258
- });
259
- }
260
- });
261
- return positions;
189
+ //#endregion
190
+ //#region src/plugin/utils/text-between.ts
191
+ const textBetween = (from, to, doc) => {
192
+ const positions = [];
193
+ doc.nodesBetween(from, to, (node, pos) => {
194
+ if (node.isText) {
195
+ var _node$text;
196
+ const offset = Math.max(from, pos) - pos;
197
+ positions.push({
198
+ pos: pos + offset,
199
+ text: ((_node$text = node.text) === null || _node$text === void 0 ? void 0 : _node$text.slice(offset, to - pos)) || ""
200
+ });
201
+ }
202
+ });
203
+ return positions;
262
204
  };
263
-
264
- // src/plugin/InvisibleCharacter.ts
205
+ //#endregion
206
+ //#region src/plugin/InvisibleCharacter.ts
265
207
  var InvisibleCharacter = class {
266
- constructor(options) {
267
- this.predicate = options.predicate;
268
- this.type = options.type;
269
- this.content = options.content;
270
- this.priority = options.priority || 100;
271
- }
272
- createDecoration(from, to, doc, decorations) {
273
- const textContent = textBetween(from, to, doc);
274
- return textContent.reduce((oldDecorations, currentPosition) => {
275
- return currentPosition.text.split("").reduce((innerDecorations, char, i) => {
276
- return this.test(char) ? innerDecorations.add(doc, [
277
- createDecorationWidget(currentPosition.pos + i, this.type, this.content)
278
- ]) : innerDecorations;
279
- }, oldDecorations);
280
- }, decorations);
281
- }
282
- test(value) {
283
- return this.predicate(value);
284
- }
208
+ constructor(options) {
209
+ this.predicate = options.predicate;
210
+ this.type = options.type;
211
+ this.content = options.content;
212
+ this.priority = options.priority || 100;
213
+ }
214
+ createDecoration(from, to, doc, decorations) {
215
+ return textBetween(from, to, doc).reduce((oldDecorations, currentPosition) => {
216
+ return currentPosition.text.split("").reduce((innerDecorations, char, i) => {
217
+ return this.test(char) ? innerDecorations.add(doc, [createDecorationWidget(currentPosition.pos + i, this.type, this.content)]) : innerDecorations;
218
+ }, oldDecorations);
219
+ }, decorations);
220
+ }
221
+ test(value) {
222
+ return this.predicate(value);
223
+ }
285
224
  };
286
-
287
- // src/plugin/invisible-characters/space.ts
225
+ //#endregion
226
+ //#region src/plugin/invisible-characters/space.ts
288
227
  var SpaceCharacter = class extends InvisibleCharacter {
289
- constructor() {
290
- super({
291
- type: "space",
292
- predicate: (char) => char === " "
293
- });
294
- }
228
+ constructor() {
229
+ super({
230
+ type: "space",
231
+ predicate: (char) => char === " "
232
+ });
233
+ }
295
234
  };
296
-
297
- // src/invisible-characters.ts
298
- var InvisibleCharacters = import_core.Extension.create({
299
- name: "invisibleCharacters",
300
- addOptions() {
301
- return {
302
- visible: true,
303
- builders: [new SpaceCharacter(), new ParagraphNode(), new HardBreakNode()],
304
- injectCSS: true,
305
- injectNonce: void 0
306
- };
307
- },
308
- addProseMirrorPlugins() {
309
- return [InvisibleCharactersPlugin(this.editor.state, this.options)];
310
- },
311
- addStorage() {
312
- return {
313
- visibility: () => this.options.visible
314
- };
315
- },
316
- onBeforeCreate() {
317
- this.storage.visibility = () => {
318
- var _a;
319
- return (_a = InvisibleCharactersPluginKey.getState(this.editor.state)) == null ? void 0 : _a.visible;
320
- };
321
- },
322
- addCommands() {
323
- return {
324
- showInvisibleCharacters: (visibility = true) => ({ dispatch, tr }) => {
325
- if (dispatch) {
326
- tr.setMeta("setInvisibleCharactersVisible", visibility);
327
- }
328
- return true;
329
- },
330
- hideInvisibleCharacters: () => ({ dispatch, tr }) => {
331
- if (dispatch) {
332
- tr.setMeta("setInvisibleCharactersVisible", false);
333
- }
334
- return true;
335
- },
336
- toggleInvisibleCharacters: () => ({ dispatch, tr, state }) => {
337
- var _a;
338
- const visibility = !((_a = InvisibleCharactersPluginKey.getState(state)) == null ? void 0 : _a.visible);
339
- if (dispatch) {
340
- tr.setMeta("setInvisibleCharactersVisible", visibility);
341
- }
342
- return true;
343
- }
344
- };
345
- }
235
+ //#endregion
236
+ //#region src/invisible-characters.ts
237
+ const InvisibleCharacters = _tiptap_core.Extension.create({
238
+ name: "invisibleCharacters",
239
+ addOptions() {
240
+ return {
241
+ visible: true,
242
+ builders: [
243
+ new SpaceCharacter(),
244
+ new ParagraphNode(),
245
+ new HardBreakNode()
246
+ ],
247
+ injectCSS: true,
248
+ injectNonce: void 0
249
+ };
250
+ },
251
+ addProseMirrorPlugins() {
252
+ return [InvisibleCharactersPlugin(this.editor.state, this.options)];
253
+ },
254
+ addStorage() {
255
+ return { visibility: () => this.options.visible };
256
+ },
257
+ onBeforeCreate() {
258
+ this.storage.visibility = () => {
259
+ var _InvisibleCharactersP;
260
+ return (_InvisibleCharactersP = InvisibleCharactersPluginKey.getState(this.editor.state)) === null || _InvisibleCharactersP === void 0 ? void 0 : _InvisibleCharactersP.visible;
261
+ };
262
+ },
263
+ addCommands() {
264
+ return {
265
+ showInvisibleCharacters: (visibility = true) => ({ dispatch, tr }) => {
266
+ if (dispatch) tr.setMeta("setInvisibleCharactersVisible", visibility);
267
+ return true;
268
+ },
269
+ hideInvisibleCharacters: () => ({ dispatch, tr }) => {
270
+ if (dispatch) tr.setMeta("setInvisibleCharactersVisible", false);
271
+ return true;
272
+ },
273
+ toggleInvisibleCharacters: () => ({ dispatch, tr, state }) => {
274
+ var _InvisibleCharactersP2;
275
+ const visibility = !((_InvisibleCharactersP2 = InvisibleCharactersPluginKey.getState(state)) === null || _InvisibleCharactersP2 === void 0 ? void 0 : _InvisibleCharactersP2.visible);
276
+ if (dispatch) tr.setMeta("setInvisibleCharactersVisible", visibility);
277
+ return true;
278
+ }
279
+ };
280
+ }
346
281
  });
282
+ //#endregion
283
+ //#region src/index.ts
284
+ var src_default = InvisibleCharacters;
285
+ //#endregion
286
+ exports.HardBreakNode = HardBreakNode;
287
+ exports.InvisibleCharacter = InvisibleCharacter;
288
+ exports.InvisibleCharacters = InvisibleCharacters;
289
+ exports.InvisibleNode = InvisibleNode;
290
+ exports.ParagraphNode = ParagraphNode;
291
+ exports.SpaceCharacter = SpaceCharacter;
292
+ exports.default = src_default;
347
293
 
348
- // src/index.ts
349
- var index_default = InvisibleCharacters;
350
- // Annotate the CommonJS export names for ESM import in node:
351
- 0 && (module.exports = {
352
- HardBreakNode,
353
- InvisibleCharacter,
354
- InvisibleCharacters,
355
- InvisibleNode,
356
- ParagraphNode,
357
- SpaceCharacter
358
- });
359
294
  //# sourceMappingURL=index.cjs.map