@tiptap/core 2.0.0-beta.155 → 2.0.0-beta.159

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.
@@ -1420,6 +1420,9 @@
1420
1420
  return true;
1421
1421
  }
1422
1422
  const before = tr.doc.resolve(Math.max(0, list.pos - 1)).before(list.depth);
1423
+ if (before === undefined) {
1424
+ return true;
1425
+ }
1423
1426
  const nodeBefore = tr.doc.nodeAt(before);
1424
1427
  const canJoinBackwards = list.node.type === (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.type)
1425
1428
  && prosemirrorTransform.canJoin(tr.doc, list.pos);
@@ -1435,6 +1438,9 @@
1435
1438
  return true;
1436
1439
  }
1437
1440
  const after = tr.doc.resolve(list.start).after(list.depth);
1441
+ if (after === undefined) {
1442
+ return true;
1443
+ }
1438
1444
  const nodeAfter = tr.doc.nodeAt(after);
1439
1445
  const canJoinForwards = list.node.type === (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.type)
1440
1446
  && prosemirrorTransform.canJoin(tr.doc, after);
@@ -1861,144 +1867,6 @@
1861
1867
  },
1862
1868
  });
1863
1869
 
1864
- const Keymap = Extension.create({
1865
- name: 'keymap',
1866
- addKeyboardShortcuts() {
1867
- const handleBackspace = () => this.editor.commands.first(({ commands }) => [
1868
- () => commands.undoInputRule(),
1869
- () => commands.deleteSelection(),
1870
- () => commands.joinBackward(),
1871
- () => commands.selectNodeBackward(),
1872
- ]);
1873
- const handleDelete = () => this.editor.commands.first(({ commands }) => [
1874
- () => commands.deleteSelection(),
1875
- () => commands.joinForward(),
1876
- () => commands.selectNodeForward(),
1877
- ]);
1878
- return {
1879
- Enter: () => this.editor.commands.first(({ commands }) => [
1880
- () => commands.newlineInCode(),
1881
- () => commands.createParagraphNear(),
1882
- () => commands.liftEmptyBlock(),
1883
- () => commands.splitBlock(),
1884
- ]),
1885
- 'Mod-Enter': () => this.editor.commands.exitCode(),
1886
- Backspace: handleBackspace,
1887
- 'Mod-Backspace': handleBackspace,
1888
- 'Shift-Backspace': handleBackspace,
1889
- Delete: handleDelete,
1890
- 'Mod-Delete': handleDelete,
1891
- 'Mod-a': () => this.editor.commands.selectAll(),
1892
- };
1893
- },
1894
- });
1895
-
1896
- const Tabindex = Extension.create({
1897
- name: 'tabindex',
1898
- addProseMirrorPlugins() {
1899
- return [
1900
- new prosemirrorState.Plugin({
1901
- key: new prosemirrorState.PluginKey('tabindex'),
1902
- props: {
1903
- attributes: {
1904
- tabindex: '0',
1905
- },
1906
- },
1907
- }),
1908
- ];
1909
- },
1910
- });
1911
-
1912
- var extensions = /*#__PURE__*/Object.freeze({
1913
- __proto__: null,
1914
- ClipboardTextSerializer: ClipboardTextSerializer,
1915
- Commands: Commands,
1916
- Editable: Editable,
1917
- FocusEvents: FocusEvents,
1918
- Keymap: Keymap,
1919
- Tabindex: Tabindex
1920
- });
1921
-
1922
- function getNodeAttributes(state, typeOrName) {
1923
- const type = getNodeType(typeOrName, state.schema);
1924
- const { from, to } = state.selection;
1925
- const nodes = [];
1926
- state.doc.nodesBetween(from, to, node => {
1927
- nodes.push(node);
1928
- });
1929
- const node = nodes
1930
- .reverse()
1931
- .find(nodeItem => nodeItem.type.name === type.name);
1932
- if (!node) {
1933
- return {};
1934
- }
1935
- return { ...node.attrs };
1936
- }
1937
-
1938
- function getAttributes(state, typeOrName) {
1939
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
1940
- ? typeOrName
1941
- : typeOrName.name, state.schema);
1942
- if (schemaType === 'node') {
1943
- return getNodeAttributes(state, typeOrName);
1944
- }
1945
- if (schemaType === 'mark') {
1946
- return getMarkAttributes(state, typeOrName);
1947
- }
1948
- return {};
1949
- }
1950
-
1951
- function isActive(state, name, attributes = {}) {
1952
- if (!name) {
1953
- return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
1954
- }
1955
- const schemaType = getSchemaTypeNameByName(name, state.schema);
1956
- if (schemaType === 'node') {
1957
- return isNodeActive(state, name, attributes);
1958
- }
1959
- if (schemaType === 'mark') {
1960
- return isMarkActive(state, name, attributes);
1961
- }
1962
- return false;
1963
- }
1964
-
1965
- function getHTMLFromFragment(fragment, schema) {
1966
- const documentFragment = prosemirrorModel.DOMSerializer
1967
- .fromSchema(schema)
1968
- .serializeFragment(fragment);
1969
- const temporaryDocument = document.implementation.createHTMLDocument();
1970
- const container = temporaryDocument.createElement('div');
1971
- container.appendChild(documentFragment);
1972
- return container.innerHTML;
1973
- }
1974
-
1975
- function getText(node, options) {
1976
- const range = {
1977
- from: 0,
1978
- to: node.content.size,
1979
- };
1980
- return getTextBetween(node, range, options);
1981
- }
1982
-
1983
- function isNodeEmpty(node) {
1984
- var _a;
1985
- const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
1986
- const content = node.toJSON();
1987
- return JSON.stringify(defaultContent) === JSON.stringify(content);
1988
- }
1989
-
1990
- function createStyleTag(style) {
1991
- const tipTapStyleTag = document.querySelector('style[data-tiptap-style]');
1992
- if (tipTapStyleTag !== null) {
1993
- return tipTapStyleTag;
1994
- }
1995
- const styleNode = document.createElement('style');
1996
- styleNode.setAttribute('data-tiptap-style', '');
1997
- styleNode.innerHTML = style;
1998
- document.getElementsByTagName('head')[0].appendChild(styleNode);
1999
- return styleNode;
2000
- }
2001
-
2002
1870
  function createChainableState(config) {
2003
1871
  const { state, transaction } = config;
2004
1872
  let { selection } = transaction;
@@ -2141,6 +2009,199 @@
2141
2009
  }
2142
2010
  }
2143
2011
 
2012
+ const Keymap = Extension.create({
2013
+ name: 'keymap',
2014
+ addKeyboardShortcuts() {
2015
+ const handleBackspace = () => this.editor.commands.first(({ commands }) => [
2016
+ () => commands.undoInputRule(),
2017
+ // maybe convert first text block node to default node
2018
+ () => commands.command(({ tr }) => {
2019
+ const { selection, doc } = tr;
2020
+ const { empty, $anchor } = selection;
2021
+ const { pos, parent } = $anchor;
2022
+ const isAtStart = prosemirrorState.Selection.atStart(doc).from === pos;
2023
+ if (!empty
2024
+ || !isAtStart
2025
+ || !parent.type.isTextblock
2026
+ || parent.textContent.length) {
2027
+ return false;
2028
+ }
2029
+ return commands.clearNodes();
2030
+ }),
2031
+ () => commands.deleteSelection(),
2032
+ () => commands.joinBackward(),
2033
+ () => commands.selectNodeBackward(),
2034
+ ]);
2035
+ const handleDelete = () => this.editor.commands.first(({ commands }) => [
2036
+ () => commands.deleteSelection(),
2037
+ () => commands.joinForward(),
2038
+ () => commands.selectNodeForward(),
2039
+ ]);
2040
+ return {
2041
+ Enter: () => this.editor.commands.first(({ commands }) => [
2042
+ () => commands.newlineInCode(),
2043
+ () => commands.createParagraphNear(),
2044
+ () => commands.liftEmptyBlock(),
2045
+ () => commands.splitBlock(),
2046
+ ]),
2047
+ 'Mod-Enter': () => this.editor.commands.exitCode(),
2048
+ Backspace: handleBackspace,
2049
+ 'Mod-Backspace': handleBackspace,
2050
+ 'Shift-Backspace': handleBackspace,
2051
+ Delete: handleDelete,
2052
+ 'Mod-Delete': handleDelete,
2053
+ 'Mod-a': () => this.editor.commands.selectAll(),
2054
+ };
2055
+ },
2056
+ addProseMirrorPlugins() {
2057
+ return [
2058
+ // With this plugin we check if the whole document was selected and deleted.
2059
+ // In this case we will additionally call `clearNodes()` to convert e.g. a heading
2060
+ // to a paragraph if necessary.
2061
+ // This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
2062
+ // with many other commands.
2063
+ new prosemirrorState.Plugin({
2064
+ key: new prosemirrorState.PluginKey('clearDocument'),
2065
+ appendTransaction: (transactions, oldState, newState) => {
2066
+ const docChanges = transactions.some(transaction => transaction.docChanged)
2067
+ && !oldState.doc.eq(newState.doc);
2068
+ if (!docChanges) {
2069
+ return;
2070
+ }
2071
+ const { empty, from, to } = oldState.selection;
2072
+ const allFrom = prosemirrorState.Selection.atStart(oldState.doc).from;
2073
+ const allEnd = prosemirrorState.Selection.atEnd(oldState.doc).to;
2074
+ const allWasSelected = from === allFrom && to === allEnd;
2075
+ const isEmpty = newState.doc.textBetween(0, newState.doc.content.size, ' ', ' ').length === 0;
2076
+ if (empty || !allWasSelected || !isEmpty) {
2077
+ return;
2078
+ }
2079
+ const tr = newState.tr;
2080
+ const state = createChainableState({
2081
+ state: newState,
2082
+ transaction: tr,
2083
+ });
2084
+ const { commands } = new CommandManager({
2085
+ editor: this.editor,
2086
+ state,
2087
+ });
2088
+ commands.clearNodes();
2089
+ if (!tr.steps.length) {
2090
+ return;
2091
+ }
2092
+ return tr;
2093
+ },
2094
+ }),
2095
+ ];
2096
+ },
2097
+ });
2098
+
2099
+ const Tabindex = Extension.create({
2100
+ name: 'tabindex',
2101
+ addProseMirrorPlugins() {
2102
+ return [
2103
+ new prosemirrorState.Plugin({
2104
+ key: new prosemirrorState.PluginKey('tabindex'),
2105
+ props: {
2106
+ attributes: {
2107
+ tabindex: '0',
2108
+ },
2109
+ },
2110
+ }),
2111
+ ];
2112
+ },
2113
+ });
2114
+
2115
+ var extensions = /*#__PURE__*/Object.freeze({
2116
+ __proto__: null,
2117
+ ClipboardTextSerializer: ClipboardTextSerializer,
2118
+ Commands: Commands,
2119
+ Editable: Editable,
2120
+ FocusEvents: FocusEvents,
2121
+ Keymap: Keymap,
2122
+ Tabindex: Tabindex
2123
+ });
2124
+
2125
+ function getNodeAttributes(state, typeOrName) {
2126
+ const type = getNodeType(typeOrName, state.schema);
2127
+ const { from, to } = state.selection;
2128
+ const nodes = [];
2129
+ state.doc.nodesBetween(from, to, node => {
2130
+ nodes.push(node);
2131
+ });
2132
+ const node = nodes
2133
+ .reverse()
2134
+ .find(nodeItem => nodeItem.type.name === type.name);
2135
+ if (!node) {
2136
+ return {};
2137
+ }
2138
+ return { ...node.attrs };
2139
+ }
2140
+
2141
+ function getAttributes(state, typeOrName) {
2142
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2143
+ ? typeOrName
2144
+ : typeOrName.name, state.schema);
2145
+ if (schemaType === 'node') {
2146
+ return getNodeAttributes(state, typeOrName);
2147
+ }
2148
+ if (schemaType === 'mark') {
2149
+ return getMarkAttributes(state, typeOrName);
2150
+ }
2151
+ return {};
2152
+ }
2153
+
2154
+ function isActive(state, name, attributes = {}) {
2155
+ if (!name) {
2156
+ return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2157
+ }
2158
+ const schemaType = getSchemaTypeNameByName(name, state.schema);
2159
+ if (schemaType === 'node') {
2160
+ return isNodeActive(state, name, attributes);
2161
+ }
2162
+ if (schemaType === 'mark') {
2163
+ return isMarkActive(state, name, attributes);
2164
+ }
2165
+ return false;
2166
+ }
2167
+
2168
+ function getHTMLFromFragment(fragment, schema) {
2169
+ const documentFragment = prosemirrorModel.DOMSerializer
2170
+ .fromSchema(schema)
2171
+ .serializeFragment(fragment);
2172
+ const temporaryDocument = document.implementation.createHTMLDocument();
2173
+ const container = temporaryDocument.createElement('div');
2174
+ container.appendChild(documentFragment);
2175
+ return container.innerHTML;
2176
+ }
2177
+
2178
+ function getText(node, options) {
2179
+ const range = {
2180
+ from: 0,
2181
+ to: node.content.size,
2182
+ };
2183
+ return getTextBetween(node, range, options);
2184
+ }
2185
+
2186
+ function isNodeEmpty(node) {
2187
+ var _a;
2188
+ const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2189
+ const content = node.toJSON();
2190
+ return JSON.stringify(defaultContent) === JSON.stringify(content);
2191
+ }
2192
+
2193
+ function createStyleTag(style) {
2194
+ const tipTapStyleTag = document.querySelector('style[data-tiptap-style]');
2195
+ if (tipTapStyleTag !== null) {
2196
+ return tipTapStyleTag;
2197
+ }
2198
+ const styleNode = document.createElement('style');
2199
+ styleNode.setAttribute('data-tiptap-style', '');
2200
+ styleNode.innerHTML = style;
2201
+ document.getElementsByTagName('head')[0].appendChild(styleNode);
2202
+ return styleNode;
2203
+ }
2204
+
2144
2205
  class InputRule {
2145
2206
  constructor(config) {
2146
2207
  this.find = config.find;
@@ -2185,7 +2246,7 @@
2185
2246
  }
2186
2247
  let matched = false;
2187
2248
  const maxMatch = 500;
2188
- const textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - maxMatch), $from.parentOffset, undefined, '\ufffc') + text;
2249
+ const textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - maxMatch), $from.parentOffset, undefined, ' ') + text;
2189
2250
  rules.forEach(rule => {
2190
2251
  if (matched) {
2191
2252
  return;
@@ -4188,12 +4249,17 @@ img.ProseMirror-separator {
4188
4249
  function getDebugJSON(node, startOffset = 0) {
4189
4250
  const isTopNode = node.type === node.type.schema.topNodeType;
4190
4251
  const increment = isTopNode ? 0 : 1;
4191
- const from = startOffset; // + offset
4252
+ const from = startOffset;
4192
4253
  const to = from + node.nodeSize;
4193
- const marks = node.marks.map(mark => ({
4194
- type: mark.type.name,
4195
- attrs: { ...mark.attrs },
4196
- }));
4254
+ const marks = node.marks.map(mark => {
4255
+ const output = {
4256
+ type: mark.type.name,
4257
+ };
4258
+ if (Object.keys(mark.attrs).length) {
4259
+ output.attrs = { ...mark.attrs };
4260
+ }
4261
+ return output;
4262
+ });
4197
4263
  const attrs = { ...node.attrs };
4198
4264
  const output = {
4199
4265
  type: node.type.name,