@tiptap/core 2.0.0-beta.195 → 2.0.0-beta.197

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.
@@ -1,8 +1,8 @@
1
1
  import { Plugin, PluginKey, TextSelection, Selection, NodeSelection, EditorState } from 'prosemirror-state';
2
2
  import { EditorView } from 'prosemirror-view';
3
3
  import { keymap } from 'prosemirror-keymap';
4
- import { Schema, Fragment, DOMParser, Slice, DOMSerializer, Node as Node$1 } from 'prosemirror-model';
5
- import { liftTarget, ReplaceStep, ReplaceAroundStep, canSplit, canJoin, Transform, findWrapping } from 'prosemirror-transform';
4
+ import { Schema, Fragment, DOMParser, DOMSerializer, Node as Node$1, Slice } from 'prosemirror-model';
5
+ import { liftTarget, ReplaceStep, ReplaceAroundStep, Transform, canSplit, canJoin, findWrapping } from 'prosemirror-transform';
6
6
  import { createParagraphNear as createParagraphNear$1, deleteSelection as deleteSelection$1, exitCode as exitCode$1, joinBackward as joinBackward$1, joinForward as joinForward$1, lift as lift$1, liftEmptyBlock as liftEmptyBlock$1, newlineInCode as newlineInCode$1, selectNodeBackward as selectNodeBackward$1, selectNodeForward as selectNodeForward$1, selectParentNode as selectParentNode$1, selectTextblockEnd as selectTextblockEnd$1, selectTextblockStart as selectTextblockStart$1, setBlockType, wrapIn as wrapIn$1 } from 'prosemirror-commands';
7
7
  import { liftListItem as liftListItem$1, sinkListItem as sinkListItem$1, wrapInList as wrapInList$1 } from 'prosemirror-schema-list';
8
8
 
@@ -1932,6 +1932,135 @@ const setContent = (content, emitUpdate = false, parseOptions = {}) => ({ tr, ed
1932
1932
  return true;
1933
1933
  };
1934
1934
 
1935
+ /**
1936
+ * Returns a new `Transform` based on all steps of the passed transactions.
1937
+ */
1938
+ function combineTransactionSteps(oldDoc, transactions) {
1939
+ const transform = new Transform(oldDoc);
1940
+ transactions.forEach(transaction => {
1941
+ transaction.steps.forEach(step => {
1942
+ transform.step(step);
1943
+ });
1944
+ });
1945
+ return transform;
1946
+ }
1947
+
1948
+ function defaultBlockAt(match) {
1949
+ for (let i = 0; i < match.edgeCount; i += 1) {
1950
+ const { type } = match.edge(i);
1951
+ if (type.isTextblock && !type.hasRequiredAttrs()) {
1952
+ return type;
1953
+ }
1954
+ }
1955
+ return null;
1956
+ }
1957
+
1958
+ function findChildren(node, predicate) {
1959
+ const nodesWithPos = [];
1960
+ node.descendants((child, pos) => {
1961
+ if (predicate(child)) {
1962
+ nodesWithPos.push({
1963
+ node: child,
1964
+ pos,
1965
+ });
1966
+ }
1967
+ });
1968
+ return nodesWithPos;
1969
+ }
1970
+
1971
+ /**
1972
+ * Same as `findChildren` but searches only within a `range`.
1973
+ */
1974
+ function findChildrenInRange(node, range, predicate) {
1975
+ const nodesWithPos = [];
1976
+ // if (range.from === range.to) {
1977
+ // const nodeAt = node.nodeAt(range.from)
1978
+ // if (nodeAt) {
1979
+ // nodesWithPos.push({
1980
+ // node: nodeAt,
1981
+ // pos: range.from,
1982
+ // })
1983
+ // }
1984
+ // }
1985
+ node.nodesBetween(range.from, range.to, (child, pos) => {
1986
+ if (predicate(child)) {
1987
+ nodesWithPos.push({
1988
+ node: child,
1989
+ pos,
1990
+ });
1991
+ }
1992
+ });
1993
+ return nodesWithPos;
1994
+ }
1995
+
1996
+ function findParentNodeClosestToPos($pos, predicate) {
1997
+ for (let i = $pos.depth; i > 0; i -= 1) {
1998
+ const node = $pos.node(i);
1999
+ if (predicate(node)) {
2000
+ return {
2001
+ pos: i > 0 ? $pos.before(i) : 0,
2002
+ start: $pos.start(i),
2003
+ depth: i,
2004
+ node,
2005
+ };
2006
+ }
2007
+ }
2008
+ }
2009
+
2010
+ function findParentNode(predicate) {
2011
+ return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2012
+ }
2013
+
2014
+ function getHTMLFromFragment(fragment, schema) {
2015
+ const documentFragment = DOMSerializer
2016
+ .fromSchema(schema)
2017
+ .serializeFragment(fragment);
2018
+ const temporaryDocument = document.implementation.createHTMLDocument();
2019
+ const container = temporaryDocument.createElement('div');
2020
+ container.appendChild(documentFragment);
2021
+ return container.innerHTML;
2022
+ }
2023
+
2024
+ function getSchema(extensions) {
2025
+ const resolvedExtensions = ExtensionManager.resolve(extensions);
2026
+ return getSchemaByResolvedExtensions(resolvedExtensions);
2027
+ }
2028
+
2029
+ function generateHTML(doc, extensions) {
2030
+ const schema = getSchema(extensions);
2031
+ const contentNode = Node$1.fromJSON(schema, doc);
2032
+ return getHTMLFromFragment(contentNode.content, schema);
2033
+ }
2034
+
2035
+ function generateJSON(html, extensions) {
2036
+ const schema = getSchema(extensions);
2037
+ const dom = elementFromString(html);
2038
+ return DOMParser.fromSchema(schema)
2039
+ .parse(dom)
2040
+ .toJSON();
2041
+ }
2042
+
2043
+ function getText(node, options) {
2044
+ const range = {
2045
+ from: 0,
2046
+ to: node.content.size,
2047
+ };
2048
+ return getTextBetween(node, range, options);
2049
+ }
2050
+
2051
+ function generateText(doc, extensions, options) {
2052
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
2053
+ const schema = getSchema(extensions);
2054
+ const contentNode = Node$1.fromJSON(schema, doc);
2055
+ return getText(contentNode, {
2056
+ blockSeparator,
2057
+ textSerializers: {
2058
+ ...textSerializers,
2059
+ ...getTextSerializersFromSchema(schema),
2060
+ },
2061
+ });
2062
+ }
2063
+
1935
2064
  function getMarkAttributes(state, typeOrName) {
1936
2065
  const type = getMarkType(typeOrName, state.schema);
1937
2066
  const { from, to, empty } = state.selection;
@@ -1954,118 +2083,454 @@ function getMarkAttributes(state, typeOrName) {
1954
2083
  return { ...mark.attrs };
1955
2084
  }
1956
2085
 
1957
- const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1958
- const { selection } = tr;
1959
- const { empty, ranges } = selection;
1960
- const type = getMarkType(typeOrName, state.schema);
1961
- if (dispatch) {
1962
- if (empty) {
1963
- const oldAttributes = getMarkAttributes(state, type);
1964
- tr.addStoredMark(type.create({
1965
- ...oldAttributes,
1966
- ...attributes,
1967
- }));
1968
- }
1969
- else {
1970
- ranges.forEach(range => {
1971
- const from = range.$from.pos;
1972
- const to = range.$to.pos;
1973
- state.doc.nodesBetween(from, to, (node, pos) => {
1974
- const trimmedFrom = Math.max(pos, from);
1975
- const trimmedTo = Math.min(pos + node.nodeSize, to);
1976
- const someHasMark = node.marks.find(mark => mark.type === type);
1977
- // if there is already a mark of this type
1978
- // we know that we have to merge its attributes
1979
- // otherwise we add a fresh new mark
1980
- if (someHasMark) {
1981
- node.marks.forEach(mark => {
1982
- if (type === mark.type) {
1983
- tr.addMark(trimmedFrom, trimmedTo, type.create({
1984
- ...mark.attrs,
1985
- ...attributes,
1986
- }));
1987
- }
1988
- });
1989
- }
1990
- else {
1991
- tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
1992
- }
1993
- });
1994
- });
1995
- }
1996
- }
1997
- return true;
1998
- };
1999
-
2000
- const setMeta = (key, value) => ({ tr }) => {
2001
- tr.setMeta(key, value);
2002
- return true;
2003
- };
2004
-
2005
- const setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
2086
+ function getNodeAttributes(state, typeOrName) {
2006
2087
  const type = getNodeType(typeOrName, state.schema);
2007
- // TODO: use a fallback like insertContent?
2008
- if (!type.isTextblock) {
2009
- console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
2010
- return false;
2011
- }
2012
- return chain()
2013
- // try to convert node to default node if needed
2014
- .command(({ commands }) => {
2015
- const canSetBlock = setBlockType(type, attributes)(state);
2016
- if (canSetBlock) {
2017
- return true;
2018
- }
2019
- return commands.clearNodes();
2020
- })
2021
- .command(({ state: updatedState }) => {
2022
- return setBlockType(type, attributes)(updatedState, dispatch);
2023
- })
2024
- .run();
2025
- };
2026
-
2027
- const setNodeSelection = position => ({ tr, dispatch }) => {
2028
- if (dispatch) {
2029
- const { doc } = tr;
2030
- const from = minMax(position, 0, doc.content.size);
2031
- const selection = NodeSelection.create(doc, from);
2032
- tr.setSelection(selection);
2088
+ const { from, to } = state.selection;
2089
+ const nodes = [];
2090
+ state.doc.nodesBetween(from, to, node => {
2091
+ nodes.push(node);
2092
+ });
2093
+ const node = nodes
2094
+ .reverse()
2095
+ .find(nodeItem => nodeItem.type.name === type.name);
2096
+ if (!node) {
2097
+ return {};
2033
2098
  }
2034
- return true;
2035
- };
2099
+ return { ...node.attrs };
2100
+ }
2036
2101
 
2037
- const setTextSelection = position => ({ tr, dispatch }) => {
2038
- if (dispatch) {
2039
- const { doc } = tr;
2040
- const { from, to } = typeof position === 'number'
2041
- ? { from: position, to: position }
2042
- : position;
2043
- const minPos = TextSelection.atStart(doc).from;
2044
- const maxPos = TextSelection.atEnd(doc).to;
2045
- const resolvedFrom = minMax(from, minPos, maxPos);
2046
- const resolvedEnd = minMax(to, minPos, maxPos);
2047
- const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd);
2048
- tr.setSelection(selection);
2102
+ function getAttributes(state, typeOrName) {
2103
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2104
+ ? typeOrName
2105
+ : typeOrName.name, state.schema);
2106
+ if (schemaType === 'node') {
2107
+ return getNodeAttributes(state, typeOrName);
2049
2108
  }
2050
- return true;
2051
- };
2052
-
2053
- const sinkListItem = typeOrName => ({ state, dispatch }) => {
2054
- const type = getNodeType(typeOrName, state.schema);
2055
- return sinkListItem$1(type)(state, dispatch);
2056
- };
2057
-
2058
- function defaultBlockAt(match) {
2059
- for (let i = 0; i < match.edgeCount; i += 1) {
2060
- const { type } = match.edge(i);
2061
- if (type.isTextblock && !type.hasRequiredAttrs()) {
2062
- return type;
2063
- }
2109
+ if (schemaType === 'mark') {
2110
+ return getMarkAttributes(state, typeOrName);
2064
2111
  }
2065
- return null;
2112
+ return {};
2066
2113
  }
2067
2114
 
2068
- function getSplittedAttributes(extensionAttributes, typeName, attributes) {
2115
+ /**
2116
+ * Removes duplicated values within an array.
2117
+ * Supports numbers, strings and objects.
2118
+ */
2119
+ function removeDuplicates(array, by = JSON.stringify) {
2120
+ const seen = {};
2121
+ return array.filter(item => {
2122
+ const key = by(item);
2123
+ return Object.prototype.hasOwnProperty.call(seen, key)
2124
+ ? false
2125
+ : (seen[key] = true);
2126
+ });
2127
+ }
2128
+
2129
+ /**
2130
+ * Removes duplicated ranges and ranges that are
2131
+ * fully captured by other ranges.
2132
+ */
2133
+ function simplifyChangedRanges(changes) {
2134
+ const uniqueChanges = removeDuplicates(changes);
2135
+ return uniqueChanges.length === 1
2136
+ ? uniqueChanges
2137
+ : uniqueChanges.filter((change, index) => {
2138
+ const rest = uniqueChanges.filter((_, i) => i !== index);
2139
+ return !rest.some(otherChange => {
2140
+ return change.oldRange.from >= otherChange.oldRange.from
2141
+ && change.oldRange.to <= otherChange.oldRange.to
2142
+ && change.newRange.from >= otherChange.newRange.from
2143
+ && change.newRange.to <= otherChange.newRange.to;
2144
+ });
2145
+ });
2146
+ }
2147
+ /**
2148
+ * Returns a list of changed ranges
2149
+ * based on the first and last state of all steps.
2150
+ */
2151
+ function getChangedRanges(transform) {
2152
+ const { mapping, steps } = transform;
2153
+ const changes = [];
2154
+ mapping.maps.forEach((stepMap, index) => {
2155
+ const ranges = [];
2156
+ // This accounts for step changes where no range was actually altered
2157
+ // e.g. when setting a mark, node attribute, etc.
2158
+ // @ts-ignore
2159
+ if (!stepMap.ranges.length) {
2160
+ const { from, to } = steps[index];
2161
+ if (from === undefined || to === undefined) {
2162
+ return;
2163
+ }
2164
+ ranges.push({ from, to });
2165
+ }
2166
+ else {
2167
+ stepMap.forEach((from, to) => {
2168
+ ranges.push({ from, to });
2169
+ });
2170
+ }
2171
+ ranges.forEach(({ from, to }) => {
2172
+ const newStart = mapping.slice(index).map(from, -1);
2173
+ const newEnd = mapping.slice(index).map(to);
2174
+ const oldStart = mapping.invert().map(newStart, -1);
2175
+ const oldEnd = mapping.invert().map(newEnd);
2176
+ changes.push({
2177
+ oldRange: {
2178
+ from: oldStart,
2179
+ to: oldEnd,
2180
+ },
2181
+ newRange: {
2182
+ from: newStart,
2183
+ to: newEnd,
2184
+ },
2185
+ });
2186
+ });
2187
+ });
2188
+ return simplifyChangedRanges(changes);
2189
+ }
2190
+
2191
+ function getDebugJSON(node, startOffset = 0) {
2192
+ const isTopNode = node.type === node.type.schema.topNodeType;
2193
+ const increment = isTopNode ? 0 : 1;
2194
+ const from = startOffset;
2195
+ const to = from + node.nodeSize;
2196
+ const marks = node.marks.map(mark => {
2197
+ const output = {
2198
+ type: mark.type.name,
2199
+ };
2200
+ if (Object.keys(mark.attrs).length) {
2201
+ output.attrs = { ...mark.attrs };
2202
+ }
2203
+ return output;
2204
+ });
2205
+ const attrs = { ...node.attrs };
2206
+ const output = {
2207
+ type: node.type.name,
2208
+ from,
2209
+ to,
2210
+ };
2211
+ if (Object.keys(attrs).length) {
2212
+ output.attrs = attrs;
2213
+ }
2214
+ if (marks.length) {
2215
+ output.marks = marks;
2216
+ }
2217
+ if (node.content.childCount) {
2218
+ output.content = [];
2219
+ node.forEach((child, offset) => {
2220
+ var _a;
2221
+ (_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
2222
+ });
2223
+ }
2224
+ if (node.text) {
2225
+ output.text = node.text;
2226
+ }
2227
+ return output;
2228
+ }
2229
+
2230
+ function getMarksBetween(from, to, doc) {
2231
+ const marks = [];
2232
+ // get all inclusive marks on empty selection
2233
+ if (from === to) {
2234
+ doc
2235
+ .resolve(from)
2236
+ .marks()
2237
+ .forEach(mark => {
2238
+ const $pos = doc.resolve(from - 1);
2239
+ const range = getMarkRange($pos, mark.type);
2240
+ if (!range) {
2241
+ return;
2242
+ }
2243
+ marks.push({
2244
+ mark,
2245
+ ...range,
2246
+ });
2247
+ });
2248
+ }
2249
+ else {
2250
+ doc.nodesBetween(from, to, (node, pos) => {
2251
+ marks.push(...node.marks.map(mark => ({
2252
+ from: pos,
2253
+ to: pos + node.nodeSize,
2254
+ mark,
2255
+ })));
2256
+ });
2257
+ }
2258
+ return marks;
2259
+ }
2260
+
2261
+ function isMarkActive(state, typeOrName, attributes = {}) {
2262
+ const { empty, ranges } = state.selection;
2263
+ const type = typeOrName
2264
+ ? getMarkType(typeOrName, state.schema)
2265
+ : null;
2266
+ if (empty) {
2267
+ return !!(state.storedMarks || state.selection.$from.marks())
2268
+ .filter(mark => {
2269
+ if (!type) {
2270
+ return true;
2271
+ }
2272
+ return type.name === mark.type.name;
2273
+ })
2274
+ .find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
2275
+ }
2276
+ let selectionRange = 0;
2277
+ const markRanges = [];
2278
+ ranges.forEach(({ $from, $to }) => {
2279
+ const from = $from.pos;
2280
+ const to = $to.pos;
2281
+ state.doc.nodesBetween(from, to, (node, pos) => {
2282
+ if (!node.isText && !node.marks.length) {
2283
+ return;
2284
+ }
2285
+ const relativeFrom = Math.max(from, pos);
2286
+ const relativeTo = Math.min(to, pos + node.nodeSize);
2287
+ const range = relativeTo - relativeFrom;
2288
+ selectionRange += range;
2289
+ markRanges.push(...node.marks.map(mark => ({
2290
+ mark,
2291
+ from: relativeFrom,
2292
+ to: relativeTo,
2293
+ })));
2294
+ });
2295
+ });
2296
+ if (selectionRange === 0) {
2297
+ return false;
2298
+ }
2299
+ // calculate range of matched mark
2300
+ const matchedRange = markRanges
2301
+ .filter(markRange => {
2302
+ if (!type) {
2303
+ return true;
2304
+ }
2305
+ return type.name === markRange.mark.type.name;
2306
+ })
2307
+ .filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
2308
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2309
+ // calculate range of marks that excludes the searched mark
2310
+ // for example `code` doesn’t allow any other marks
2311
+ const excludedRange = markRanges
2312
+ .filter(markRange => {
2313
+ if (!type) {
2314
+ return true;
2315
+ }
2316
+ return markRange.mark.type !== type
2317
+ && markRange.mark.type.excludes(type);
2318
+ })
2319
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2320
+ // we only include the result of `excludedRange`
2321
+ // if there is a match at all
2322
+ const range = matchedRange > 0
2323
+ ? matchedRange + excludedRange
2324
+ : matchedRange;
2325
+ return range >= selectionRange;
2326
+ }
2327
+
2328
+ function isActive(state, name, attributes = {}) {
2329
+ if (!name) {
2330
+ return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2331
+ }
2332
+ const schemaType = getSchemaTypeNameByName(name, state.schema);
2333
+ if (schemaType === 'node') {
2334
+ return isNodeActive(state, name, attributes);
2335
+ }
2336
+ if (schemaType === 'mark') {
2337
+ return isMarkActive(state, name, attributes);
2338
+ }
2339
+ return false;
2340
+ }
2341
+
2342
+ function isList(name, extensions) {
2343
+ const { nodeExtensions } = splitExtensions(extensions);
2344
+ const extension = nodeExtensions.find(item => item.name === name);
2345
+ if (!extension) {
2346
+ return false;
2347
+ }
2348
+ const context = {
2349
+ name: extension.name,
2350
+ options: extension.options,
2351
+ storage: extension.storage,
2352
+ };
2353
+ const group = callOrReturn(getExtensionField(extension, 'group', context));
2354
+ if (typeof group !== 'string') {
2355
+ return false;
2356
+ }
2357
+ return group.split(' ').includes('list');
2358
+ }
2359
+
2360
+ function isNodeEmpty(node) {
2361
+ var _a;
2362
+ const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2363
+ const content = node.toJSON();
2364
+ return JSON.stringify(defaultContent) === JSON.stringify(content);
2365
+ }
2366
+
2367
+ function isNodeSelection(value) {
2368
+ return value instanceof NodeSelection;
2369
+ }
2370
+
2371
+ function posToDOMRect(view, from, to) {
2372
+ const minPos = 0;
2373
+ const maxPos = view.state.doc.content.size;
2374
+ const resolvedFrom = minMax(from, minPos, maxPos);
2375
+ const resolvedEnd = minMax(to, minPos, maxPos);
2376
+ const start = view.coordsAtPos(resolvedFrom);
2377
+ const end = view.coordsAtPos(resolvedEnd, -1);
2378
+ const top = Math.min(start.top, end.top);
2379
+ const bottom = Math.max(start.bottom, end.bottom);
2380
+ const left = Math.min(start.left, end.left);
2381
+ const right = Math.max(start.right, end.right);
2382
+ const width = right - left;
2383
+ const height = bottom - top;
2384
+ const x = left;
2385
+ const y = top;
2386
+ const data = {
2387
+ top,
2388
+ bottom,
2389
+ left,
2390
+ right,
2391
+ width,
2392
+ height,
2393
+ x,
2394
+ y,
2395
+ };
2396
+ return {
2397
+ ...data,
2398
+ toJSON: () => data,
2399
+ };
2400
+ }
2401
+
2402
+ function canSetMark(state, tr, newMarkType) {
2403
+ var _a;
2404
+ const { selection } = tr;
2405
+ let cursor = null;
2406
+ if (isTextSelection(selection)) {
2407
+ cursor = selection.$cursor;
2408
+ }
2409
+ if (cursor) {
2410
+ const currentMarks = (_a = state.storedMarks) !== null && _a !== void 0 ? _a : cursor.marks();
2411
+ // There can be no current marks that exclude the new mark
2412
+ return !!newMarkType.isInSet(currentMarks) || !currentMarks.some(mark => mark.type.excludes(newMarkType));
2413
+ }
2414
+ const { ranges } = selection;
2415
+ return ranges.some(({ $from, $to }) => {
2416
+ let someNodeSupportsMark = $from.depth === 0 ? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType) : false;
2417
+ state.doc.nodesBetween($from.pos, $to.pos, (node, _pos, parent) => {
2418
+ // If we already found a mark that we can enable, return false to bypass the remaining search
2419
+ if (someNodeSupportsMark) {
2420
+ return false;
2421
+ }
2422
+ if (node.isInline) {
2423
+ const parentAllowsMarkType = !parent || parent.type.allowsMarkType(newMarkType);
2424
+ const currentMarksAllowMarkType = !!newMarkType.isInSet(node.marks) || !node.marks.some(otherMark => otherMark.type.excludes(newMarkType));
2425
+ someNodeSupportsMark = parentAllowsMarkType && currentMarksAllowMarkType;
2426
+ }
2427
+ return !someNodeSupportsMark;
2428
+ });
2429
+ return someNodeSupportsMark;
2430
+ });
2431
+ }
2432
+ const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
2433
+ const { selection } = tr;
2434
+ const { empty, ranges } = selection;
2435
+ const type = getMarkType(typeOrName, state.schema);
2436
+ if (dispatch) {
2437
+ if (empty) {
2438
+ const oldAttributes = getMarkAttributes(state, type);
2439
+ tr.addStoredMark(type.create({
2440
+ ...oldAttributes,
2441
+ ...attributes,
2442
+ }));
2443
+ }
2444
+ else {
2445
+ ranges.forEach(range => {
2446
+ const from = range.$from.pos;
2447
+ const to = range.$to.pos;
2448
+ state.doc.nodesBetween(from, to, (node, pos) => {
2449
+ const trimmedFrom = Math.max(pos, from);
2450
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
2451
+ const someHasMark = node.marks.find(mark => mark.type === type);
2452
+ // if there is already a mark of this type
2453
+ // we know that we have to merge its attributes
2454
+ // otherwise we add a fresh new mark
2455
+ if (someHasMark) {
2456
+ node.marks.forEach(mark => {
2457
+ if (type === mark.type) {
2458
+ tr.addMark(trimmedFrom, trimmedTo, type.create({
2459
+ ...mark.attrs,
2460
+ ...attributes,
2461
+ }));
2462
+ }
2463
+ });
2464
+ }
2465
+ else {
2466
+ tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
2467
+ }
2468
+ });
2469
+ });
2470
+ }
2471
+ }
2472
+ return canSetMark(state, tr, type);
2473
+ };
2474
+
2475
+ const setMeta = (key, value) => ({ tr }) => {
2476
+ tr.setMeta(key, value);
2477
+ return true;
2478
+ };
2479
+
2480
+ const setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
2481
+ const type = getNodeType(typeOrName, state.schema);
2482
+ // TODO: use a fallback like insertContent?
2483
+ if (!type.isTextblock) {
2484
+ console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
2485
+ return false;
2486
+ }
2487
+ return chain()
2488
+ // try to convert node to default node if needed
2489
+ .command(({ commands }) => {
2490
+ const canSetBlock = setBlockType(type, attributes)(state);
2491
+ if (canSetBlock) {
2492
+ return true;
2493
+ }
2494
+ return commands.clearNodes();
2495
+ })
2496
+ .command(({ state: updatedState }) => {
2497
+ return setBlockType(type, attributes)(updatedState, dispatch);
2498
+ })
2499
+ .run();
2500
+ };
2501
+
2502
+ const setNodeSelection = position => ({ tr, dispatch }) => {
2503
+ if (dispatch) {
2504
+ const { doc } = tr;
2505
+ const from = minMax(position, 0, doc.content.size);
2506
+ const selection = NodeSelection.create(doc, from);
2507
+ tr.setSelection(selection);
2508
+ }
2509
+ return true;
2510
+ };
2511
+
2512
+ const setTextSelection = position => ({ tr, dispatch }) => {
2513
+ if (dispatch) {
2514
+ const { doc } = tr;
2515
+ const { from, to } = typeof position === 'number'
2516
+ ? { from: position, to: position }
2517
+ : position;
2518
+ const minPos = TextSelection.atStart(doc).from;
2519
+ const maxPos = TextSelection.atEnd(doc).to;
2520
+ const resolvedFrom = minMax(from, minPos, maxPos);
2521
+ const resolvedEnd = minMax(to, minPos, maxPos);
2522
+ const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd);
2523
+ tr.setSelection(selection);
2524
+ }
2525
+ return true;
2526
+ };
2527
+
2528
+ const sinkListItem = typeOrName => ({ state, dispatch }) => {
2529
+ const type = getNodeType(typeOrName, state.schema);
2530
+ return sinkListItem$1(type)(state, dispatch);
2531
+ };
2532
+
2533
+ function getSplittedAttributes(extensionAttributes, typeName, attributes) {
2069
2534
  return Object.fromEntries(Object
2070
2535
  .entries(attributes)
2071
2536
  .filter(([name]) => {
@@ -2237,42 +2702,6 @@ const splitListItem = typeOrName => ({ tr, state, dispatch, editor, }) => {
2237
2702
  return true;
2238
2703
  };
2239
2704
 
2240
- function findParentNodeClosestToPos($pos, predicate) {
2241
- for (let i = $pos.depth; i > 0; i -= 1) {
2242
- const node = $pos.node(i);
2243
- if (predicate(node)) {
2244
- return {
2245
- pos: i > 0 ? $pos.before(i) : 0,
2246
- start: $pos.start(i),
2247
- depth: i,
2248
- node,
2249
- };
2250
- }
2251
- }
2252
- }
2253
-
2254
- function findParentNode(predicate) {
2255
- return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2256
- }
2257
-
2258
- function isList(name, extensions) {
2259
- const { nodeExtensions } = splitExtensions(extensions);
2260
- const extension = nodeExtensions.find(item => item.name === name);
2261
- if (!extension) {
2262
- return false;
2263
- }
2264
- const context = {
2265
- name: extension.name,
2266
- options: extension.options,
2267
- storage: extension.storage,
2268
- };
2269
- const group = callOrReturn(getExtensionField(extension, 'group', context));
2270
- if (typeof group !== 'string') {
2271
- return false;
2272
- }
2273
- return group.split(' ').includes('list');
2274
- }
2275
-
2276
2705
  const joinListBackwards = (tr, listType) => {
2277
2706
  const list = findParentNode(node => node.type === listType)(tr.selection);
2278
2707
  if (!list) {
@@ -2354,73 +2783,6 @@ const toggleList = (listTypeOrName, itemTypeOrName) => ({ editor, tr, state, dis
2354
2783
  .run();
2355
2784
  };
2356
2785
 
2357
- function isMarkActive(state, typeOrName, attributes = {}) {
2358
- const { empty, ranges } = state.selection;
2359
- const type = typeOrName
2360
- ? getMarkType(typeOrName, state.schema)
2361
- : null;
2362
- if (empty) {
2363
- return !!(state.storedMarks || state.selection.$from.marks())
2364
- .filter(mark => {
2365
- if (!type) {
2366
- return true;
2367
- }
2368
- return type.name === mark.type.name;
2369
- })
2370
- .find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
2371
- }
2372
- let selectionRange = 0;
2373
- const markRanges = [];
2374
- ranges.forEach(({ $from, $to }) => {
2375
- const from = $from.pos;
2376
- const to = $to.pos;
2377
- state.doc.nodesBetween(from, to, (node, pos) => {
2378
- if (!node.isText && !node.marks.length) {
2379
- return;
2380
- }
2381
- const relativeFrom = Math.max(from, pos);
2382
- const relativeTo = Math.min(to, pos + node.nodeSize);
2383
- const range = relativeTo - relativeFrom;
2384
- selectionRange += range;
2385
- markRanges.push(...node.marks.map(mark => ({
2386
- mark,
2387
- from: relativeFrom,
2388
- to: relativeTo,
2389
- })));
2390
- });
2391
- });
2392
- if (selectionRange === 0) {
2393
- return false;
2394
- }
2395
- // calculate range of matched mark
2396
- const matchedRange = markRanges
2397
- .filter(markRange => {
2398
- if (!type) {
2399
- return true;
2400
- }
2401
- return type.name === markRange.mark.type.name;
2402
- })
2403
- .filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
2404
- .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2405
- // calculate range of marks that excludes the searched mark
2406
- // for example `code` doesn’t allow any other marks
2407
- const excludedRange = markRanges
2408
- .filter(markRange => {
2409
- if (!type) {
2410
- return true;
2411
- }
2412
- return markRange.mark.type !== type
2413
- && markRange.mark.type.excludes(type);
2414
- })
2415
- .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2416
- // we only include the result of `excludedRange`
2417
- // if there is a match at all
2418
- const range = matchedRange > 0
2419
- ? matchedRange + excludedRange
2420
- : matchedRange;
2421
- return range >= selectionRange;
2422
- }
2423
-
2424
2786
  const toggleMark = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
2425
2787
  const { extendEmptyMarkRange = false } = options;
2426
2788
  const type = getMarkType(typeOrName, state.schema);
@@ -2808,78 +3170,10 @@ var extensions = /*#__PURE__*/Object.freeze({
2808
3170
  ClipboardTextSerializer: ClipboardTextSerializer,
2809
3171
  Commands: Commands,
2810
3172
  Editable: Editable,
2811
- FocusEvents: FocusEvents,
2812
- Keymap: Keymap,
2813
- Tabindex: Tabindex
2814
- });
2815
-
2816
- function getNodeAttributes(state, typeOrName) {
2817
- const type = getNodeType(typeOrName, state.schema);
2818
- const { from, to } = state.selection;
2819
- const nodes = [];
2820
- state.doc.nodesBetween(from, to, node => {
2821
- nodes.push(node);
2822
- });
2823
- const node = nodes
2824
- .reverse()
2825
- .find(nodeItem => nodeItem.type.name === type.name);
2826
- if (!node) {
2827
- return {};
2828
- }
2829
- return { ...node.attrs };
2830
- }
2831
-
2832
- function getAttributes(state, typeOrName) {
2833
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2834
- ? typeOrName
2835
- : typeOrName.name, state.schema);
2836
- if (schemaType === 'node') {
2837
- return getNodeAttributes(state, typeOrName);
2838
- }
2839
- if (schemaType === 'mark') {
2840
- return getMarkAttributes(state, typeOrName);
2841
- }
2842
- return {};
2843
- }
2844
-
2845
- function getHTMLFromFragment(fragment, schema) {
2846
- const documentFragment = DOMSerializer
2847
- .fromSchema(schema)
2848
- .serializeFragment(fragment);
2849
- const temporaryDocument = document.implementation.createHTMLDocument();
2850
- const container = temporaryDocument.createElement('div');
2851
- container.appendChild(documentFragment);
2852
- return container.innerHTML;
2853
- }
2854
-
2855
- function getText(node, options) {
2856
- const range = {
2857
- from: 0,
2858
- to: node.content.size,
2859
- };
2860
- return getTextBetween(node, range, options);
2861
- }
2862
-
2863
- function isActive(state, name, attributes = {}) {
2864
- if (!name) {
2865
- return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2866
- }
2867
- const schemaType = getSchemaTypeNameByName(name, state.schema);
2868
- if (schemaType === 'node') {
2869
- return isNodeActive(state, name, attributes);
2870
- }
2871
- if (schemaType === 'mark') {
2872
- return isMarkActive(state, name, attributes);
2873
- }
2874
- return false;
2875
- }
2876
-
2877
- function isNodeEmpty(node) {
2878
- var _a;
2879
- const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2880
- const content = node.toJSON();
2881
- return JSON.stringify(defaultContent) === JSON.stringify(content);
2882
- }
3173
+ FocusEvents: FocusEvents,
3174
+ Keymap: Keymap,
3175
+ Tabindex: Tabindex
3176
+ });
2883
3177
 
2884
3178
  const style = `.ProseMirror {
2885
3179
  position: relative;
@@ -3185,407 +3479,143 @@ class Editor extends EventEmitter {
3185
3479
  /**
3186
3480
  * Creates all node views.
3187
3481
  */
3188
- createNodeViews() {
3189
- this.view.setProps({
3190
- nodeViews: this.extensionManager.nodeViews,
3191
- });
3192
- }
3193
- captureTransaction(fn) {
3194
- this.isCapturingTransaction = true;
3195
- fn();
3196
- this.isCapturingTransaction = false;
3197
- const tr = this.capturedTransaction;
3198
- this.capturedTransaction = null;
3199
- return tr;
3200
- }
3201
- /**
3202
- * The callback over which to send transactions (state updates) produced by the view.
3203
- *
3204
- * @param transaction An editor state transaction
3205
- */
3206
- dispatchTransaction(transaction) {
3207
- if (this.isCapturingTransaction) {
3208
- if (!this.capturedTransaction) {
3209
- this.capturedTransaction = transaction;
3210
- return;
3211
- }
3212
- transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
3213
- return;
3214
- }
3215
- const state = this.state.apply(transaction);
3216
- const selectionHasChanged = !this.state.selection.eq(state.selection);
3217
- this.view.updateState(state);
3218
- this.emit('transaction', {
3219
- editor: this,
3220
- transaction,
3221
- });
3222
- if (selectionHasChanged) {
3223
- this.emit('selectionUpdate', {
3224
- editor: this,
3225
- transaction,
3226
- });
3227
- }
3228
- const focus = transaction.getMeta('focus');
3229
- const blur = transaction.getMeta('blur');
3230
- if (focus) {
3231
- this.emit('focus', {
3232
- editor: this,
3233
- event: focus.event,
3234
- transaction,
3235
- });
3236
- }
3237
- if (blur) {
3238
- this.emit('blur', {
3239
- editor: this,
3240
- event: blur.event,
3241
- transaction,
3242
- });
3243
- }
3244
- if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
3245
- return;
3246
- }
3247
- this.emit('update', {
3248
- editor: this,
3249
- transaction,
3250
- });
3251
- }
3252
- /**
3253
- * Get attributes of the currently selected node or mark.
3254
- */
3255
- getAttributes(nameOrType) {
3256
- return getAttributes(this.state, nameOrType);
3257
- }
3258
- isActive(nameOrAttributes, attributesOrUndefined) {
3259
- const name = typeof nameOrAttributes === 'string'
3260
- ? nameOrAttributes
3261
- : null;
3262
- const attributes = typeof nameOrAttributes === 'string'
3263
- ? attributesOrUndefined
3264
- : nameOrAttributes;
3265
- return isActive(this.state, name, attributes);
3266
- }
3267
- /**
3268
- * Get the document as JSON.
3269
- */
3270
- getJSON() {
3271
- return this.state.doc.toJSON();
3272
- }
3273
- /**
3274
- * Get the document as HTML.
3275
- */
3276
- getHTML() {
3277
- return getHTMLFromFragment(this.state.doc.content, this.schema);
3278
- }
3279
- /**
3280
- * Get the document as text.
3281
- */
3282
- getText(options) {
3283
- const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3284
- return getText(this.state.doc, {
3285
- blockSeparator,
3286
- textSerializers: {
3287
- ...textSerializers,
3288
- ...getTextSerializersFromSchema(this.schema),
3289
- },
3290
- });
3291
- }
3292
- /**
3293
- * Check if there is no content.
3294
- */
3295
- get isEmpty() {
3296
- return isNodeEmpty(this.state.doc);
3297
- }
3298
- /**
3299
- * Get the number of characters for the current document.
3300
- *
3301
- * @deprecated
3302
- */
3303
- getCharacterCount() {
3304
- console.warn('[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.');
3305
- return this.state.doc.content.size - 2;
3306
- }
3307
- /**
3308
- * Destroy the editor.
3309
- */
3310
- destroy() {
3311
- this.emit('destroy');
3312
- if (this.view) {
3313
- this.view.destroy();
3314
- }
3315
- this.removeAllListeners();
3316
- }
3317
- /**
3318
- * Check if the editor is already destroyed.
3319
- */
3320
- get isDestroyed() {
3321
- var _a;
3322
- // @ts-ignore
3323
- return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3324
- }
3325
- }
3326
-
3327
- /**
3328
- * Returns a new `Transform` based on all steps of the passed transactions.
3329
- */
3330
- function combineTransactionSteps(oldDoc, transactions) {
3331
- const transform = new Transform(oldDoc);
3332
- transactions.forEach(transaction => {
3333
- transaction.steps.forEach(step => {
3334
- transform.step(step);
3335
- });
3336
- });
3337
- return transform;
3338
- }
3339
-
3340
- function findChildren(node, predicate) {
3341
- const nodesWithPos = [];
3342
- node.descendants((child, pos) => {
3343
- if (predicate(child)) {
3344
- nodesWithPos.push({
3345
- node: child,
3346
- pos,
3347
- });
3348
- }
3349
- });
3350
- return nodesWithPos;
3351
- }
3352
-
3353
- /**
3354
- * Same as `findChildren` but searches only within a `range`.
3355
- */
3356
- function findChildrenInRange(node, range, predicate) {
3357
- const nodesWithPos = [];
3358
- // if (range.from === range.to) {
3359
- // const nodeAt = node.nodeAt(range.from)
3360
- // if (nodeAt) {
3361
- // nodesWithPos.push({
3362
- // node: nodeAt,
3363
- // pos: range.from,
3364
- // })
3365
- // }
3366
- // }
3367
- node.nodesBetween(range.from, range.to, (child, pos) => {
3368
- if (predicate(child)) {
3369
- nodesWithPos.push({
3370
- node: child,
3371
- pos,
3372
- });
3373
- }
3374
- });
3375
- return nodesWithPos;
3376
- }
3377
-
3378
- function getSchema(extensions) {
3379
- const resolvedExtensions = ExtensionManager.resolve(extensions);
3380
- return getSchemaByResolvedExtensions(resolvedExtensions);
3381
- }
3382
-
3383
- function generateHTML(doc, extensions) {
3384
- const schema = getSchema(extensions);
3385
- const contentNode = Node$1.fromJSON(schema, doc);
3386
- return getHTMLFromFragment(contentNode.content, schema);
3387
- }
3388
-
3389
- function generateJSON(html, extensions) {
3390
- const schema = getSchema(extensions);
3391
- const dom = elementFromString(html);
3392
- return DOMParser.fromSchema(schema)
3393
- .parse(dom)
3394
- .toJSON();
3395
- }
3396
-
3397
- function generateText(doc, extensions, options) {
3398
- const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3399
- const schema = getSchema(extensions);
3400
- const contentNode = Node$1.fromJSON(schema, doc);
3401
- return getText(contentNode, {
3402
- blockSeparator,
3403
- textSerializers: {
3404
- ...textSerializers,
3405
- ...getTextSerializersFromSchema(schema),
3406
- },
3407
- });
3408
- }
3409
-
3410
- /**
3411
- * Removes duplicated values within an array.
3412
- * Supports numbers, strings and objects.
3413
- */
3414
- function removeDuplicates(array, by = JSON.stringify) {
3415
- const seen = {};
3416
- return array.filter(item => {
3417
- const key = by(item);
3418
- return Object.prototype.hasOwnProperty.call(seen, key)
3419
- ? false
3420
- : (seen[key] = true);
3421
- });
3422
- }
3423
-
3424
- /**
3425
- * Removes duplicated ranges and ranges that are
3426
- * fully captured by other ranges.
3427
- */
3428
- function simplifyChangedRanges(changes) {
3429
- const uniqueChanges = removeDuplicates(changes);
3430
- return uniqueChanges.length === 1
3431
- ? uniqueChanges
3432
- : uniqueChanges.filter((change, index) => {
3433
- const rest = uniqueChanges.filter((_, i) => i !== index);
3434
- return !rest.some(otherChange => {
3435
- return change.oldRange.from >= otherChange.oldRange.from
3436
- && change.oldRange.to <= otherChange.oldRange.to
3437
- && change.newRange.from >= otherChange.newRange.from
3438
- && change.newRange.to <= otherChange.newRange.to;
3439
- });
3482
+ createNodeViews() {
3483
+ this.view.setProps({
3484
+ nodeViews: this.extensionManager.nodeViews,
3440
3485
  });
3441
- }
3442
- /**
3443
- * Returns a list of changed ranges
3444
- * based on the first and last state of all steps.
3445
- */
3446
- function getChangedRanges(transform) {
3447
- const { mapping, steps } = transform;
3448
- const changes = [];
3449
- mapping.maps.forEach((stepMap, index) => {
3450
- const ranges = [];
3451
- // This accounts for step changes where no range was actually altered
3452
- // e.g. when setting a mark, node attribute, etc.
3453
- // @ts-ignore
3454
- if (!stepMap.ranges.length) {
3455
- const { from, to } = steps[index];
3456
- if (from === undefined || to === undefined) {
3486
+ }
3487
+ captureTransaction(fn) {
3488
+ this.isCapturingTransaction = true;
3489
+ fn();
3490
+ this.isCapturingTransaction = false;
3491
+ const tr = this.capturedTransaction;
3492
+ this.capturedTransaction = null;
3493
+ return tr;
3494
+ }
3495
+ /**
3496
+ * The callback over which to send transactions (state updates) produced by the view.
3497
+ *
3498
+ * @param transaction An editor state transaction
3499
+ */
3500
+ dispatchTransaction(transaction) {
3501
+ if (this.isCapturingTransaction) {
3502
+ if (!this.capturedTransaction) {
3503
+ this.capturedTransaction = transaction;
3457
3504
  return;
3458
3505
  }
3459
- ranges.push({ from, to });
3506
+ transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
3507
+ return;
3460
3508
  }
3461
- else {
3462
- stepMap.forEach((from, to) => {
3463
- ranges.push({ from, to });
3509
+ const state = this.state.apply(transaction);
3510
+ const selectionHasChanged = !this.state.selection.eq(state.selection);
3511
+ this.view.updateState(state);
3512
+ this.emit('transaction', {
3513
+ editor: this,
3514
+ transaction,
3515
+ });
3516
+ if (selectionHasChanged) {
3517
+ this.emit('selectionUpdate', {
3518
+ editor: this,
3519
+ transaction,
3464
3520
  });
3465
3521
  }
3466
- ranges.forEach(({ from, to }) => {
3467
- const newStart = mapping.slice(index).map(from, -1);
3468
- const newEnd = mapping.slice(index).map(to);
3469
- const oldStart = mapping.invert().map(newStart, -1);
3470
- const oldEnd = mapping.invert().map(newEnd);
3471
- changes.push({
3472
- oldRange: {
3473
- from: oldStart,
3474
- to: oldEnd,
3475
- },
3476
- newRange: {
3477
- from: newStart,
3478
- to: newEnd,
3479
- },
3522
+ const focus = transaction.getMeta('focus');
3523
+ const blur = transaction.getMeta('blur');
3524
+ if (focus) {
3525
+ this.emit('focus', {
3526
+ editor: this,
3527
+ event: focus.event,
3528
+ transaction,
3480
3529
  });
3481
- });
3482
- });
3483
- return simplifyChangedRanges(changes);
3484
- }
3485
-
3486
- function getDebugJSON(node, startOffset = 0) {
3487
- const isTopNode = node.type === node.type.schema.topNodeType;
3488
- const increment = isTopNode ? 0 : 1;
3489
- const from = startOffset;
3490
- const to = from + node.nodeSize;
3491
- const marks = node.marks.map(mark => {
3492
- const output = {
3493
- type: mark.type.name,
3494
- };
3495
- if (Object.keys(mark.attrs).length) {
3496
- output.attrs = { ...mark.attrs };
3497
3530
  }
3498
- return output;
3499
- });
3500
- const attrs = { ...node.attrs };
3501
- const output = {
3502
- type: node.type.name,
3503
- from,
3504
- to,
3505
- };
3506
- if (Object.keys(attrs).length) {
3507
- output.attrs = attrs;
3531
+ if (blur) {
3532
+ this.emit('blur', {
3533
+ editor: this,
3534
+ event: blur.event,
3535
+ transaction,
3536
+ });
3537
+ }
3538
+ if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
3539
+ return;
3540
+ }
3541
+ this.emit('update', {
3542
+ editor: this,
3543
+ transaction,
3544
+ });
3508
3545
  }
3509
- if (marks.length) {
3510
- output.marks = marks;
3546
+ /**
3547
+ * Get attributes of the currently selected node or mark.
3548
+ */
3549
+ getAttributes(nameOrType) {
3550
+ return getAttributes(this.state, nameOrType);
3511
3551
  }
3512
- if (node.content.childCount) {
3513
- output.content = [];
3514
- node.forEach((child, offset) => {
3515
- var _a;
3516
- (_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
3517
- });
3552
+ isActive(nameOrAttributes, attributesOrUndefined) {
3553
+ const name = typeof nameOrAttributes === 'string'
3554
+ ? nameOrAttributes
3555
+ : null;
3556
+ const attributes = typeof nameOrAttributes === 'string'
3557
+ ? attributesOrUndefined
3558
+ : nameOrAttributes;
3559
+ return isActive(this.state, name, attributes);
3518
3560
  }
3519
- if (node.text) {
3520
- output.text = node.text;
3561
+ /**
3562
+ * Get the document as JSON.
3563
+ */
3564
+ getJSON() {
3565
+ return this.state.doc.toJSON();
3521
3566
  }
3522
- return output;
3523
- }
3524
-
3525
- function getMarksBetween(from, to, doc) {
3526
- const marks = [];
3527
- // get all inclusive marks on empty selection
3528
- if (from === to) {
3529
- doc
3530
- .resolve(from)
3531
- .marks()
3532
- .forEach(mark => {
3533
- const $pos = doc.resolve(from - 1);
3534
- const range = getMarkRange($pos, mark.type);
3535
- if (!range) {
3536
- return;
3537
- }
3538
- marks.push({
3539
- mark,
3540
- ...range,
3541
- });
3542
- });
3567
+ /**
3568
+ * Get the document as HTML.
3569
+ */
3570
+ getHTML() {
3571
+ return getHTMLFromFragment(this.state.doc.content, this.schema);
3543
3572
  }
3544
- else {
3545
- doc.nodesBetween(from, to, (node, pos) => {
3546
- marks.push(...node.marks.map(mark => ({
3547
- from: pos,
3548
- to: pos + node.nodeSize,
3549
- mark,
3550
- })));
3573
+ /**
3574
+ * Get the document as text.
3575
+ */
3576
+ getText(options) {
3577
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3578
+ return getText(this.state.doc, {
3579
+ blockSeparator,
3580
+ textSerializers: {
3581
+ ...textSerializers,
3582
+ ...getTextSerializersFromSchema(this.schema),
3583
+ },
3551
3584
  });
3552
3585
  }
3553
- return marks;
3554
- }
3555
-
3556
- function isNodeSelection(value) {
3557
- return value instanceof NodeSelection;
3558
- }
3559
-
3560
- function posToDOMRect(view, from, to) {
3561
- const minPos = 0;
3562
- const maxPos = view.state.doc.content.size;
3563
- const resolvedFrom = minMax(from, minPos, maxPos);
3564
- const resolvedEnd = minMax(to, minPos, maxPos);
3565
- const start = view.coordsAtPos(resolvedFrom);
3566
- const end = view.coordsAtPos(resolvedEnd, -1);
3567
- const top = Math.min(start.top, end.top);
3568
- const bottom = Math.max(start.bottom, end.bottom);
3569
- const left = Math.min(start.left, end.left);
3570
- const right = Math.max(start.right, end.right);
3571
- const width = right - left;
3572
- const height = bottom - top;
3573
- const x = left;
3574
- const y = top;
3575
- const data = {
3576
- top,
3577
- bottom,
3578
- left,
3579
- right,
3580
- width,
3581
- height,
3582
- x,
3583
- y,
3584
- };
3585
- return {
3586
- ...data,
3587
- toJSON: () => data,
3588
- };
3586
+ /**
3587
+ * Check if there is no content.
3588
+ */
3589
+ get isEmpty() {
3590
+ return isNodeEmpty(this.state.doc);
3591
+ }
3592
+ /**
3593
+ * Get the number of characters for the current document.
3594
+ *
3595
+ * @deprecated
3596
+ */
3597
+ getCharacterCount() {
3598
+ console.warn('[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.');
3599
+ return this.state.doc.content.size - 2;
3600
+ }
3601
+ /**
3602
+ * Destroy the editor.
3603
+ */
3604
+ destroy() {
3605
+ this.emit('destroy');
3606
+ if (this.view) {
3607
+ this.view.destroy();
3608
+ }
3609
+ this.removeAllListeners();
3610
+ }
3611
+ /**
3612
+ * Check if the editor is already destroyed.
3613
+ */
3614
+ get isDestroyed() {
3615
+ var _a;
3616
+ // @ts-ignore
3617
+ return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3618
+ }
3589
3619
  }
3590
3620
 
3591
3621
  /**