@tiptap/extension-task-item 3.0.0-next.3 → 3.0.0-next.5

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025, Tiptap GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,14 +1,18 @@
1
1
  # @tiptap/extension-task-item
2
+
2
3
  [![Version](https://img.shields.io/npm/v/@tiptap/extension-task-item.svg?label=version)](https://www.npmjs.com/package/@tiptap/extension-task-item)
3
4
  [![Downloads](https://img.shields.io/npm/dm/@tiptap/extension-task-item.svg)](https://npmcharts.com/compare/tiptap?minimal=true)
4
5
  [![License](https://img.shields.io/npm/l/@tiptap/extension-task-item.svg)](https://www.npmjs.com/package/@tiptap/extension-task-item)
5
6
  [![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/ueberdosis)
6
7
 
7
8
  ## Introduction
8
- Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
9
+
10
+ Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
9
11
 
10
12
  ## Official Documentation
13
+
11
14
  Documentation can be found on the [Tiptap website](https://tiptap.dev).
12
15
 
13
16
  ## License
17
+
14
18
  Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
package/dist/index.cjs CHANGED
@@ -18,175 +18,19 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- TaskItem: () => TaskItem,
24
- default: () => src_default,
25
- inputRegex: () => inputRegex
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ TaskItem: () => import_extension_list2.TaskItem,
24
+ TaskItemOptions: () => import_extension_list2.TaskItemOptions,
25
+ default: () => index_default
26
26
  });
27
- module.exports = __toCommonJS(src_exports);
28
-
29
- // src/task-item.ts
30
- var import_core = require("@tiptap/core");
31
- var inputRegex = /^\s*(\[([( |x])?\])\s$/;
32
- var TaskItem = import_core.Node.create({
33
- name: "taskItem",
34
- addOptions() {
35
- return {
36
- nested: false,
37
- HTMLAttributes: {},
38
- taskListTypeName: "taskList"
39
- };
40
- },
41
- content() {
42
- return this.options.nested ? "paragraph block*" : "paragraph+";
43
- },
44
- defining: true,
45
- addAttributes() {
46
- return {
47
- checked: {
48
- default: false,
49
- keepOnSplit: false,
50
- parseHTML: (element) => {
51
- const dataChecked = element.getAttribute("data-checked");
52
- return dataChecked === "" || dataChecked === "true";
53
- },
54
- renderHTML: (attributes) => ({
55
- "data-checked": attributes.checked
56
- })
57
- }
58
- };
59
- },
60
- parseHTML() {
61
- return [
62
- {
63
- tag: `li[data-type="${this.name}"]`,
64
- priority: 51
65
- }
66
- ];
67
- },
68
- renderHTML({ node, HTMLAttributes }) {
69
- return [
70
- "li",
71
- (0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes, {
72
- "data-type": this.name
73
- }),
74
- [
75
- "label",
76
- [
77
- "input",
78
- {
79
- type: "checkbox",
80
- checked: node.attrs.checked ? "checked" : null
81
- }
82
- ],
83
- ["span"]
84
- ],
85
- ["div", 0]
86
- ];
87
- },
88
- addKeyboardShortcuts() {
89
- const shortcuts = {
90
- Enter: () => this.editor.commands.splitListItem(this.name),
91
- "Shift-Tab": () => this.editor.commands.liftListItem(this.name)
92
- };
93
- if (!this.options.nested) {
94
- return shortcuts;
95
- }
96
- return {
97
- ...shortcuts,
98
- Tab: () => this.editor.commands.sinkListItem(this.name)
99
- };
100
- },
101
- addNodeView() {
102
- return ({
103
- node,
104
- HTMLAttributes,
105
- getPos,
106
- editor
107
- }) => {
108
- const listItem = document.createElement("li");
109
- const checkboxWrapper = document.createElement("label");
110
- const checkboxStyler = document.createElement("span");
111
- const checkbox = document.createElement("input");
112
- const content = document.createElement("div");
113
- checkboxWrapper.contentEditable = "false";
114
- checkbox.type = "checkbox";
115
- checkbox.addEventListener("mousedown", (event) => event.preventDefault());
116
- checkbox.addEventListener("change", (event) => {
117
- if (!editor.isEditable && !this.options.onReadOnlyChecked) {
118
- checkbox.checked = !checkbox.checked;
119
- return;
120
- }
121
- const { checked } = event.target;
122
- if (editor.isEditable && typeof getPos === "function") {
123
- editor.chain().focus(void 0, { scrollIntoView: false }).command(({ tr }) => {
124
- const position = getPos();
125
- if (typeof position !== "number") {
126
- return false;
127
- }
128
- const currentNode = tr.doc.nodeAt(position);
129
- tr.setNodeMarkup(position, void 0, {
130
- ...currentNode == null ? void 0 : currentNode.attrs,
131
- checked
132
- });
133
- return true;
134
- }).run();
135
- }
136
- if (!editor.isEditable && this.options.onReadOnlyChecked) {
137
- if (!this.options.onReadOnlyChecked(node, checked)) {
138
- checkbox.checked = !checkbox.checked;
139
- }
140
- }
141
- });
142
- Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {
143
- listItem.setAttribute(key, value);
144
- });
145
- listItem.dataset.checked = node.attrs.checked;
146
- if (node.attrs.checked) {
147
- checkbox.setAttribute("checked", "checked");
148
- }
149
- checkboxWrapper.append(checkbox, checkboxStyler);
150
- listItem.append(checkboxWrapper, content);
151
- Object.entries(HTMLAttributes).forEach(([key, value]) => {
152
- listItem.setAttribute(key, value);
153
- });
154
- return {
155
- dom: listItem,
156
- contentDOM: content,
157
- update: (updatedNode) => {
158
- if (updatedNode.type !== this.type) {
159
- return false;
160
- }
161
- listItem.dataset.checked = updatedNode.attrs.checked;
162
- if (updatedNode.attrs.checked) {
163
- checkbox.setAttribute("checked", "checked");
164
- } else {
165
- checkbox.removeAttribute("checked");
166
- }
167
- return true;
168
- }
169
- };
170
- };
171
- },
172
- addInputRules() {
173
- return [
174
- (0, import_core.wrappingInputRule)({
175
- find: inputRegex,
176
- type: this.type,
177
- getAttributes: (match) => ({
178
- checked: match[match.length - 1] === "x"
179
- })
180
- })
181
- ];
182
- }
183
- });
184
-
185
- // src/index.ts
186
- var src_default = TaskItem;
27
+ module.exports = __toCommonJS(index_exports);
28
+ var import_extension_list = require("@tiptap/extension-list");
29
+ var import_extension_list2 = require("@tiptap/extension-list");
30
+ var index_default = import_extension_list.TaskItem;
187
31
  // Annotate the CommonJS export names for ESM import in node:
188
32
  0 && (module.exports = {
189
33
  TaskItem,
190
- inputRegex
34
+ TaskItemOptions
191
35
  });
192
36
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/task-item.ts"],"sourcesContent":["import { TaskItem } from './task-item.js'\n\nexport * from './task-item.js'\n\nexport default TaskItem\n","import {\n KeyboardShortcutCommand, mergeAttributes, Node, wrappingInputRule,\n} from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\n\nexport interface TaskItemOptions {\n /**\n * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.\n * @param node The prosemirror node of the task item\n * @param checked The new checked state\n * @returns boolean\n */\n onReadOnlyChecked?: (node: ProseMirrorNode, checked: boolean) => boolean\n\n /**\n * Controls whether the task items can be nested or not.\n * @default false\n * @example true\n */\n nested: boolean\n\n /**\n * HTML attributes to add to the task item element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * The node type for taskList nodes\n * @default 'taskList'\n * @example 'myCustomTaskList'\n */\n taskListTypeName: string\n}\n\n/**\n * Matches a task item to a - [ ] on input.\n */\nexport const inputRegex = /^\\s*(\\[([( |x])?\\])\\s$/\n\n/**\n * This extension allows you to create task items.\n * @see https://www.tiptap.dev/api/nodes/task-item\n */\nexport const TaskItem = Node.create<TaskItemOptions>({\n name: 'taskItem',\n\n addOptions() {\n return {\n nested: false,\n HTMLAttributes: {},\n taskListTypeName: 'taskList',\n }\n },\n\n content() {\n return this.options.nested ? 'paragraph block*' : 'paragraph+'\n },\n\n defining: true,\n\n addAttributes() {\n return {\n checked: {\n default: false,\n keepOnSplit: false,\n parseHTML: element => {\n const dataChecked = element.getAttribute('data-checked')\n\n return dataChecked === '' || dataChecked === 'true'\n },\n renderHTML: attributes => ({\n 'data-checked': attributes.checked,\n }),\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: `li[data-type=\"${this.name}\"]`,\n priority: 51,\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'li',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {\n 'data-type': this.name,\n }),\n [\n 'label',\n [\n 'input',\n {\n type: 'checkbox',\n checked: node.attrs.checked ? 'checked' : null,\n },\n ],\n ['span'],\n ],\n ['div', 0],\n ]\n },\n\n addKeyboardShortcuts() {\n const shortcuts: {\n [key: string]: KeyboardShortcutCommand\n } = {\n Enter: () => this.editor.commands.splitListItem(this.name),\n 'Shift-Tab': () => this.editor.commands.liftListItem(this.name),\n }\n\n if (!this.options.nested) {\n return shortcuts\n }\n\n return {\n ...shortcuts,\n Tab: () => this.editor.commands.sinkListItem(this.name),\n }\n },\n\n addNodeView() {\n return ({\n node, HTMLAttributes, getPos, editor,\n }) => {\n const listItem = document.createElement('li')\n const checkboxWrapper = document.createElement('label')\n const checkboxStyler = document.createElement('span')\n const checkbox = document.createElement('input')\n const content = document.createElement('div')\n\n checkboxWrapper.contentEditable = 'false'\n checkbox.type = 'checkbox'\n checkbox.addEventListener('mousedown', event => event.preventDefault())\n checkbox.addEventListener('change', event => {\n // if the editor isn’t editable and we don't have a handler for\n // readonly checks we have to undo the latest change\n if (!editor.isEditable && !this.options.onReadOnlyChecked) {\n checkbox.checked = !checkbox.checked\n\n return\n }\n\n const { checked } = event.target as any\n\n if (editor.isEditable && typeof getPos === 'function') {\n editor\n .chain()\n .focus(undefined, { scrollIntoView: false })\n .command(({ tr }) => {\n const position = getPos()\n\n if (typeof position !== 'number') {\n return false\n }\n const currentNode = tr.doc.nodeAt(position)\n\n tr.setNodeMarkup(position, undefined, {\n ...currentNode?.attrs,\n checked,\n })\n\n return true\n })\n .run()\n }\n if (!editor.isEditable && this.options.onReadOnlyChecked) {\n // Reset state if onReadOnlyChecked returns false\n if (!this.options.onReadOnlyChecked(node, checked)) {\n checkbox.checked = !checkbox.checked\n }\n }\n })\n\n Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {\n listItem.setAttribute(key, value)\n })\n\n listItem.dataset.checked = node.attrs.checked\n if (node.attrs.checked) {\n checkbox.setAttribute('checked', 'checked')\n }\n\n checkboxWrapper.append(checkbox, checkboxStyler)\n listItem.append(checkboxWrapper, content)\n\n Object.entries(HTMLAttributes).forEach(([key, value]) => {\n listItem.setAttribute(key, value)\n })\n\n return {\n dom: listItem,\n contentDOM: content,\n update: updatedNode => {\n if (updatedNode.type !== this.type) {\n return false\n }\n\n listItem.dataset.checked = updatedNode.attrs.checked\n if (updatedNode.attrs.checked) {\n checkbox.setAttribute('checked', 'checked')\n } else {\n checkbox.removeAttribute('checked')\n }\n\n return true\n },\n }\n }\n },\n\n addInputRules() {\n return [\n wrappingInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => ({\n checked: match[match.length - 1] === 'x',\n }),\n }),\n ]\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAEO;AAqCA,IAAM,aAAa;AAMnB,IAAM,WAAW,iBAAK,OAAwB;AAAA,EACnD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,gBAAgB,CAAC;AAAA,MACjB,kBAAkB;AAAA,IACpB;AAAA,EACF;AAAA,EAEA,UAAU;AACR,WAAO,KAAK,QAAQ,SAAS,qBAAqB;AAAA,EACpD;AAAA,EAEA,UAAU;AAAA,EAEV,gBAAgB;AACd,WAAO;AAAA,MACL,SAAS;AAAA,QACP,SAAS;AAAA,QACT,aAAa;AAAA,QACb,WAAW,aAAW;AACpB,gBAAM,cAAc,QAAQ,aAAa,cAAc;AAEvD,iBAAO,gBAAgB,MAAM,gBAAgB;AAAA,QAC/C;AAAA,QACA,YAAY,iBAAe;AAAA,UACzB,gBAAgB,WAAW;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,YAAY;AACV,WAAO;AAAA,MACL;AAAA,QACE,KAAK,iBAAiB,KAAK,IAAI;AAAA,QAC/B,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAAA,EAEA,WAAW,EAAE,MAAM,eAAe,GAAG;AACnC,WAAO;AAAA,MACL;AAAA,UACA,6BAAgB,KAAK,QAAQ,gBAAgB,gBAAgB;AAAA,QAC3D,aAAa,KAAK;AAAA,MACpB,CAA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A,IACF;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,UACL,+BAAkB;AAAA,QAChB,MAAM;AAAA,QACN,MAAM,KAAK;AAAA,QACX,eAAe,YAAU;AAAA,UACvB,SAAS,MAAM,MAAM,SAAS,CAAC,MAAM;AAAA,QACvC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ADhOD,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { TaskItem } from '@tiptap/extension-list'\n\nexport { TaskItem, TaskItemOptions } from '@tiptap/extension-list'\n\nexport default TaskItem\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAyB;AAEzB,IAAAA,yBAA0C;AAE1C,IAAO,gBAAQ;","names":["import_extension_list"]}
package/dist/index.d.cts CHANGED
@@ -1,41 +1,2 @@
1
- import { Node as Node$1 } from '@tiptap/core';
2
- import { Node } from '@tiptap/pm/model';
3
-
4
- interface TaskItemOptions {
5
- /**
6
- * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.
7
- * @param node The prosemirror node of the task item
8
- * @param checked The new checked state
9
- * @returns boolean
10
- */
11
- onReadOnlyChecked?: (node: Node, checked: boolean) => boolean;
12
- /**
13
- * Controls whether the task items can be nested or not.
14
- * @default false
15
- * @example true
16
- */
17
- nested: boolean;
18
- /**
19
- * HTML attributes to add to the task item element.
20
- * @default {}
21
- * @example { class: 'foo' }
22
- */
23
- HTMLAttributes: Record<string, any>;
24
- /**
25
- * The node type for taskList nodes
26
- * @default 'taskList'
27
- * @example 'myCustomTaskList'
28
- */
29
- taskListTypeName: string;
30
- }
31
- /**
32
- * Matches a task item to a - [ ] on input.
33
- */
34
- declare const inputRegex: RegExp;
35
- /**
36
- * This extension allows you to create task items.
37
- * @see https://www.tiptap.dev/api/nodes/task-item
38
- */
39
- declare const TaskItem: Node$1<TaskItemOptions, any>;
40
-
41
- export { TaskItem, type TaskItemOptions, TaskItem as default, inputRegex };
1
+ import { TaskItem } from '@tiptap/extension-list';
2
+ export { TaskItem, TaskItemOptions, TaskItem as default } from '@tiptap/extension-list';
package/dist/index.d.ts CHANGED
@@ -1,41 +1,2 @@
1
- import { Node as Node$1 } from '@tiptap/core';
2
- import { Node } from '@tiptap/pm/model';
3
-
4
- interface TaskItemOptions {
5
- /**
6
- * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.
7
- * @param node The prosemirror node of the task item
8
- * @param checked The new checked state
9
- * @returns boolean
10
- */
11
- onReadOnlyChecked?: (node: Node, checked: boolean) => boolean;
12
- /**
13
- * Controls whether the task items can be nested or not.
14
- * @default false
15
- * @example true
16
- */
17
- nested: boolean;
18
- /**
19
- * HTML attributes to add to the task item element.
20
- * @default {}
21
- * @example { class: 'foo' }
22
- */
23
- HTMLAttributes: Record<string, any>;
24
- /**
25
- * The node type for taskList nodes
26
- * @default 'taskList'
27
- * @example 'myCustomTaskList'
28
- */
29
- taskListTypeName: string;
30
- }
31
- /**
32
- * Matches a task item to a - [ ] on input.
33
- */
34
- declare const inputRegex: RegExp;
35
- /**
36
- * This extension allows you to create task items.
37
- * @see https://www.tiptap.dev/api/nodes/task-item
38
- */
39
- declare const TaskItem: Node$1<TaskItemOptions, any>;
40
-
41
- export { TaskItem, type TaskItemOptions, TaskItem as default, inputRegex };
1
+ import { TaskItem } from '@tiptap/extension-list';
2
+ export { TaskItem, TaskItemOptions, TaskItem as default } from '@tiptap/extension-list';
package/dist/index.js CHANGED
@@ -1,168 +1,10 @@
1
- // src/task-item.ts
2
- import {
3
- mergeAttributes,
4
- Node,
5
- wrappingInputRule
6
- } from "@tiptap/core";
7
- var inputRegex = /^\s*(\[([( |x])?\])\s$/;
8
- var TaskItem = Node.create({
9
- name: "taskItem",
10
- addOptions() {
11
- return {
12
- nested: false,
13
- HTMLAttributes: {},
14
- taskListTypeName: "taskList"
15
- };
16
- },
17
- content() {
18
- return this.options.nested ? "paragraph block*" : "paragraph+";
19
- },
20
- defining: true,
21
- addAttributes() {
22
- return {
23
- checked: {
24
- default: false,
25
- keepOnSplit: false,
26
- parseHTML: (element) => {
27
- const dataChecked = element.getAttribute("data-checked");
28
- return dataChecked === "" || dataChecked === "true";
29
- },
30
- renderHTML: (attributes) => ({
31
- "data-checked": attributes.checked
32
- })
33
- }
34
- };
35
- },
36
- parseHTML() {
37
- return [
38
- {
39
- tag: `li[data-type="${this.name}"]`,
40
- priority: 51
41
- }
42
- ];
43
- },
44
- renderHTML({ node, HTMLAttributes }) {
45
- return [
46
- "li",
47
- mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
48
- "data-type": this.name
49
- }),
50
- [
51
- "label",
52
- [
53
- "input",
54
- {
55
- type: "checkbox",
56
- checked: node.attrs.checked ? "checked" : null
57
- }
58
- ],
59
- ["span"]
60
- ],
61
- ["div", 0]
62
- ];
63
- },
64
- addKeyboardShortcuts() {
65
- const shortcuts = {
66
- Enter: () => this.editor.commands.splitListItem(this.name),
67
- "Shift-Tab": () => this.editor.commands.liftListItem(this.name)
68
- };
69
- if (!this.options.nested) {
70
- return shortcuts;
71
- }
72
- return {
73
- ...shortcuts,
74
- Tab: () => this.editor.commands.sinkListItem(this.name)
75
- };
76
- },
77
- addNodeView() {
78
- return ({
79
- node,
80
- HTMLAttributes,
81
- getPos,
82
- editor
83
- }) => {
84
- const listItem = document.createElement("li");
85
- const checkboxWrapper = document.createElement("label");
86
- const checkboxStyler = document.createElement("span");
87
- const checkbox = document.createElement("input");
88
- const content = document.createElement("div");
89
- checkboxWrapper.contentEditable = "false";
90
- checkbox.type = "checkbox";
91
- checkbox.addEventListener("mousedown", (event) => event.preventDefault());
92
- checkbox.addEventListener("change", (event) => {
93
- if (!editor.isEditable && !this.options.onReadOnlyChecked) {
94
- checkbox.checked = !checkbox.checked;
95
- return;
96
- }
97
- const { checked } = event.target;
98
- if (editor.isEditable && typeof getPos === "function") {
99
- editor.chain().focus(void 0, { scrollIntoView: false }).command(({ tr }) => {
100
- const position = getPos();
101
- if (typeof position !== "number") {
102
- return false;
103
- }
104
- const currentNode = tr.doc.nodeAt(position);
105
- tr.setNodeMarkup(position, void 0, {
106
- ...currentNode == null ? void 0 : currentNode.attrs,
107
- checked
108
- });
109
- return true;
110
- }).run();
111
- }
112
- if (!editor.isEditable && this.options.onReadOnlyChecked) {
113
- if (!this.options.onReadOnlyChecked(node, checked)) {
114
- checkbox.checked = !checkbox.checked;
115
- }
116
- }
117
- });
118
- Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {
119
- listItem.setAttribute(key, value);
120
- });
121
- listItem.dataset.checked = node.attrs.checked;
122
- if (node.attrs.checked) {
123
- checkbox.setAttribute("checked", "checked");
124
- }
125
- checkboxWrapper.append(checkbox, checkboxStyler);
126
- listItem.append(checkboxWrapper, content);
127
- Object.entries(HTMLAttributes).forEach(([key, value]) => {
128
- listItem.setAttribute(key, value);
129
- });
130
- return {
131
- dom: listItem,
132
- contentDOM: content,
133
- update: (updatedNode) => {
134
- if (updatedNode.type !== this.type) {
135
- return false;
136
- }
137
- listItem.dataset.checked = updatedNode.attrs.checked;
138
- if (updatedNode.attrs.checked) {
139
- checkbox.setAttribute("checked", "checked");
140
- } else {
141
- checkbox.removeAttribute("checked");
142
- }
143
- return true;
144
- }
145
- };
146
- };
147
- },
148
- addInputRules() {
149
- return [
150
- wrappingInputRule({
151
- find: inputRegex,
152
- type: this.type,
153
- getAttributes: (match) => ({
154
- checked: match[match.length - 1] === "x"
155
- })
156
- })
157
- ];
158
- }
159
- });
160
-
161
1
  // src/index.ts
162
- var src_default = TaskItem;
2
+ import { TaskItem } from "@tiptap/extension-list";
3
+ import { TaskItem as TaskItem2, TaskItemOptions } from "@tiptap/extension-list";
4
+ var index_default = TaskItem;
163
5
  export {
164
- TaskItem,
165
- src_default as default,
166
- inputRegex
6
+ TaskItem2 as TaskItem,
7
+ TaskItemOptions,
8
+ index_default as default
167
9
  };
168
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/task-item.ts","../src/index.ts"],"sourcesContent":["import {\n KeyboardShortcutCommand, mergeAttributes, Node, wrappingInputRule,\n} from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\n\nexport interface TaskItemOptions {\n /**\n * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.\n * @param node The prosemirror node of the task item\n * @param checked The new checked state\n * @returns boolean\n */\n onReadOnlyChecked?: (node: ProseMirrorNode, checked: boolean) => boolean\n\n /**\n * Controls whether the task items can be nested or not.\n * @default false\n * @example true\n */\n nested: boolean\n\n /**\n * HTML attributes to add to the task item element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * The node type for taskList nodes\n * @default 'taskList'\n * @example 'myCustomTaskList'\n */\n taskListTypeName: string\n}\n\n/**\n * Matches a task item to a - [ ] on input.\n */\nexport const inputRegex = /^\\s*(\\[([( |x])?\\])\\s$/\n\n/**\n * This extension allows you to create task items.\n * @see https://www.tiptap.dev/api/nodes/task-item\n */\nexport const TaskItem = Node.create<TaskItemOptions>({\n name: 'taskItem',\n\n addOptions() {\n return {\n nested: false,\n HTMLAttributes: {},\n taskListTypeName: 'taskList',\n }\n },\n\n content() {\n return this.options.nested ? 'paragraph block*' : 'paragraph+'\n },\n\n defining: true,\n\n addAttributes() {\n return {\n checked: {\n default: false,\n keepOnSplit: false,\n parseHTML: element => {\n const dataChecked = element.getAttribute('data-checked')\n\n return dataChecked === '' || dataChecked === 'true'\n },\n renderHTML: attributes => ({\n 'data-checked': attributes.checked,\n }),\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: `li[data-type=\"${this.name}\"]`,\n priority: 51,\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'li',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {\n 'data-type': this.name,\n }),\n [\n 'label',\n [\n 'input',\n {\n type: 'checkbox',\n checked: node.attrs.checked ? 'checked' : null,\n },\n ],\n ['span'],\n ],\n ['div', 0],\n ]\n },\n\n addKeyboardShortcuts() {\n const shortcuts: {\n [key: string]: KeyboardShortcutCommand\n } = {\n Enter: () => this.editor.commands.splitListItem(this.name),\n 'Shift-Tab': () => this.editor.commands.liftListItem(this.name),\n }\n\n if (!this.options.nested) {\n return shortcuts\n }\n\n return {\n ...shortcuts,\n Tab: () => this.editor.commands.sinkListItem(this.name),\n }\n },\n\n addNodeView() {\n return ({\n node, HTMLAttributes, getPos, editor,\n }) => {\n const listItem = document.createElement('li')\n const checkboxWrapper = document.createElement('label')\n const checkboxStyler = document.createElement('span')\n const checkbox = document.createElement('input')\n const content = document.createElement('div')\n\n checkboxWrapper.contentEditable = 'false'\n checkbox.type = 'checkbox'\n checkbox.addEventListener('mousedown', event => event.preventDefault())\n checkbox.addEventListener('change', event => {\n // if the editor isn’t editable and we don't have a handler for\n // readonly checks we have to undo the latest change\n if (!editor.isEditable && !this.options.onReadOnlyChecked) {\n checkbox.checked = !checkbox.checked\n\n return\n }\n\n const { checked } = event.target as any\n\n if (editor.isEditable && typeof getPos === 'function') {\n editor\n .chain()\n .focus(undefined, { scrollIntoView: false })\n .command(({ tr }) => {\n const position = getPos()\n\n if (typeof position !== 'number') {\n return false\n }\n const currentNode = tr.doc.nodeAt(position)\n\n tr.setNodeMarkup(position, undefined, {\n ...currentNode?.attrs,\n checked,\n })\n\n return true\n })\n .run()\n }\n if (!editor.isEditable && this.options.onReadOnlyChecked) {\n // Reset state if onReadOnlyChecked returns false\n if (!this.options.onReadOnlyChecked(node, checked)) {\n checkbox.checked = !checkbox.checked\n }\n }\n })\n\n Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {\n listItem.setAttribute(key, value)\n })\n\n listItem.dataset.checked = node.attrs.checked\n if (node.attrs.checked) {\n checkbox.setAttribute('checked', 'checked')\n }\n\n checkboxWrapper.append(checkbox, checkboxStyler)\n listItem.append(checkboxWrapper, content)\n\n Object.entries(HTMLAttributes).forEach(([key, value]) => {\n listItem.setAttribute(key, value)\n })\n\n return {\n dom: listItem,\n contentDOM: content,\n update: updatedNode => {\n if (updatedNode.type !== this.type) {\n return false\n }\n\n listItem.dataset.checked = updatedNode.attrs.checked\n if (updatedNode.attrs.checked) {\n checkbox.setAttribute('checked', 'checked')\n } else {\n checkbox.removeAttribute('checked')\n }\n\n return true\n },\n }\n }\n },\n\n addInputRules() {\n return [\n wrappingInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => ({\n checked: match[match.length - 1] === 'x',\n }),\n }),\n ]\n },\n})\n","import { TaskItem } from './task-item.js'\n\nexport * from './task-item.js'\n\nexport default TaskItem\n"],"mappings":";AAAA;AAAA,EAC2B;AAAA,EAAiB;AAAA,EAAM;AAAA,OAC3C;AAqCA,IAAM,aAAa;AAMnB,IAAM,WAAW,KAAK,OAAwB;AAAA,EACnD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,gBAAgB,CAAC;AAAA,MACjB,kBAAkB;AAAA,IACpB;AAAA,EACF;AAAA,EAEA,UAAU;AACR,WAAO,KAAK,QAAQ,SAAS,qBAAqB;AAAA,EACpD;AAAA,EAEA,UAAU;AAAA,EAEV,gBAAgB;AACd,WAAO;AAAA,MACL,SAAS;AAAA,QACP,SAAS;AAAA,QACT,aAAa;AAAA,QACb,WAAW,aAAW;AACpB,gBAAM,cAAc,QAAQ,aAAa,cAAc;AAEvD,iBAAO,gBAAgB,MAAM,gBAAgB;AAAA,QAC/C;AAAA,QACA,YAAY,iBAAe;AAAA,UACzB,gBAAgB,WAAW;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,YAAY;AACV,WAAO;AAAA,MACL;AAAA,QACE,KAAK,iBAAiB,KAAK,IAAI;AAAA,QAC/B,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAAA,EAEA,WAAW,EAAE,MAAM,eAAe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BAAS,aAAa,WAAW,SAAS;AAAA,UAC5C,OAAO;AACL,qBAAS,gBAAgB,SAAS;AAAA,UACpC;AAEA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,kBAAkB;AAAA,QAChB,MAAM;AAAA,QACN,MAAM,KAAK;AAAA,QACX,eAAe,YAAU;AAAA,UACvB,SAAS,MAAM,MAAM,SAAS,CAAC,MAAM;AAAA,QACvC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AChOD,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { TaskItem } from '@tiptap/extension-list'\n\nexport { TaskItem, TaskItemOptions } from '@tiptap/extension-list'\n\nexport default TaskItem\n"],"mappings":";AAAA,SAAS,gBAAgB;AAEzB,SAAS,YAAAA,WAAU,uBAAuB;AAE1C,IAAO,gBAAQ;","names":["TaskItem"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-task-item",
3
3
  "description": "task item extension for tiptap",
4
- "version": "3.0.0-next.3",
4
+ "version": "3.0.0-next.5",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -15,7 +15,10 @@
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.d.ts",
18
+ "types": {
19
+ "import": "./dist/index.d.ts",
20
+ "require": "./dist/index.d.cts"
21
+ },
19
22
  "import": "./dist/index.js",
20
23
  "require": "./dist/index.cjs"
21
24
  }
@@ -28,12 +31,10 @@
28
31
  "dist"
29
32
  ],
30
33
  "devDependencies": {
31
- "@tiptap/core": "^3.0.0-next.3",
32
- "@tiptap/pm": "^3.0.0-next.3"
34
+ "@tiptap/extension-list": "^3.0.0-next.5"
33
35
  },
34
36
  "peerDependencies": {
35
- "@tiptap/core": "^3.0.0-next.1",
36
- "@tiptap/pm": "^3.0.0-next.1"
37
+ "@tiptap/extension-list": "^3.0.0-next.4"
37
38
  },
38
39
  "repository": {
39
40
  "type": "git",
@@ -41,6 +42,7 @@
41
42
  "directory": "packages/extension-task-item"
42
43
  },
43
44
  "scripts": {
44
- "build": "tsup"
45
+ "build": "tsup",
46
+ "lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
45
47
  }
46
- }
48
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { TaskItem } from './task-item.js'
1
+ import { TaskItem } from '@tiptap/extension-list'
2
2
 
3
- export * from './task-item.js'
3
+ export { TaskItem, TaskItemOptions } from '@tiptap/extension-list'
4
4
 
5
5
  export default TaskItem
package/src/task-item.ts DELETED
@@ -1,229 +0,0 @@
1
- import {
2
- KeyboardShortcutCommand, mergeAttributes, Node, wrappingInputRule,
3
- } from '@tiptap/core'
4
- import { Node as ProseMirrorNode } from '@tiptap/pm/model'
5
-
6
- export interface TaskItemOptions {
7
- /**
8
- * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.
9
- * @param node The prosemirror node of the task item
10
- * @param checked The new checked state
11
- * @returns boolean
12
- */
13
- onReadOnlyChecked?: (node: ProseMirrorNode, checked: boolean) => boolean
14
-
15
- /**
16
- * Controls whether the task items can be nested or not.
17
- * @default false
18
- * @example true
19
- */
20
- nested: boolean
21
-
22
- /**
23
- * HTML attributes to add to the task item element.
24
- * @default {}
25
- * @example { class: 'foo' }
26
- */
27
- HTMLAttributes: Record<string, any>
28
-
29
- /**
30
- * The node type for taskList nodes
31
- * @default 'taskList'
32
- * @example 'myCustomTaskList'
33
- */
34
- taskListTypeName: string
35
- }
36
-
37
- /**
38
- * Matches a task item to a - [ ] on input.
39
- */
40
- export const inputRegex = /^\s*(\[([( |x])?\])\s$/
41
-
42
- /**
43
- * This extension allows you to create task items.
44
- * @see https://www.tiptap.dev/api/nodes/task-item
45
- */
46
- export const TaskItem = Node.create<TaskItemOptions>({
47
- name: 'taskItem',
48
-
49
- addOptions() {
50
- return {
51
- nested: false,
52
- HTMLAttributes: {},
53
- taskListTypeName: 'taskList',
54
- }
55
- },
56
-
57
- content() {
58
- return this.options.nested ? 'paragraph block*' : 'paragraph+'
59
- },
60
-
61
- defining: true,
62
-
63
- addAttributes() {
64
- return {
65
- checked: {
66
- default: false,
67
- keepOnSplit: false,
68
- parseHTML: element => {
69
- const dataChecked = element.getAttribute('data-checked')
70
-
71
- return dataChecked === '' || dataChecked === 'true'
72
- },
73
- renderHTML: attributes => ({
74
- 'data-checked': attributes.checked,
75
- }),
76
- },
77
- }
78
- },
79
-
80
- parseHTML() {
81
- return [
82
- {
83
- tag: `li[data-type="${this.name}"]`,
84
- priority: 51,
85
- },
86
- ]
87
- },
88
-
89
- renderHTML({ node, HTMLAttributes }) {
90
- return [
91
- 'li',
92
- mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
93
- 'data-type': this.name,
94
- }),
95
- [
96
- 'label',
97
- [
98
- 'input',
99
- {
100
- type: 'checkbox',
101
- checked: node.attrs.checked ? 'checked' : null,
102
- },
103
- ],
104
- ['span'],
105
- ],
106
- ['div', 0],
107
- ]
108
- },
109
-
110
- addKeyboardShortcuts() {
111
- const shortcuts: {
112
- [key: string]: KeyboardShortcutCommand
113
- } = {
114
- Enter: () => this.editor.commands.splitListItem(this.name),
115
- 'Shift-Tab': () => this.editor.commands.liftListItem(this.name),
116
- }
117
-
118
- if (!this.options.nested) {
119
- return shortcuts
120
- }
121
-
122
- return {
123
- ...shortcuts,
124
- Tab: () => this.editor.commands.sinkListItem(this.name),
125
- }
126
- },
127
-
128
- addNodeView() {
129
- return ({
130
- node, HTMLAttributes, getPos, editor,
131
- }) => {
132
- const listItem = document.createElement('li')
133
- const checkboxWrapper = document.createElement('label')
134
- const checkboxStyler = document.createElement('span')
135
- const checkbox = document.createElement('input')
136
- const content = document.createElement('div')
137
-
138
- checkboxWrapper.contentEditable = 'false'
139
- checkbox.type = 'checkbox'
140
- checkbox.addEventListener('mousedown', event => event.preventDefault())
141
- checkbox.addEventListener('change', event => {
142
- // if the editor isn’t editable and we don't have a handler for
143
- // readonly checks we have to undo the latest change
144
- if (!editor.isEditable && !this.options.onReadOnlyChecked) {
145
- checkbox.checked = !checkbox.checked
146
-
147
- return
148
- }
149
-
150
- const { checked } = event.target as any
151
-
152
- if (editor.isEditable && typeof getPos === 'function') {
153
- editor
154
- .chain()
155
- .focus(undefined, { scrollIntoView: false })
156
- .command(({ tr }) => {
157
- const position = getPos()
158
-
159
- if (typeof position !== 'number') {
160
- return false
161
- }
162
- const currentNode = tr.doc.nodeAt(position)
163
-
164
- tr.setNodeMarkup(position, undefined, {
165
- ...currentNode?.attrs,
166
- checked,
167
- })
168
-
169
- return true
170
- })
171
- .run()
172
- }
173
- if (!editor.isEditable && this.options.onReadOnlyChecked) {
174
- // Reset state if onReadOnlyChecked returns false
175
- if (!this.options.onReadOnlyChecked(node, checked)) {
176
- checkbox.checked = !checkbox.checked
177
- }
178
- }
179
- })
180
-
181
- Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {
182
- listItem.setAttribute(key, value)
183
- })
184
-
185
- listItem.dataset.checked = node.attrs.checked
186
- if (node.attrs.checked) {
187
- checkbox.setAttribute('checked', 'checked')
188
- }
189
-
190
- checkboxWrapper.append(checkbox, checkboxStyler)
191
- listItem.append(checkboxWrapper, content)
192
-
193
- Object.entries(HTMLAttributes).forEach(([key, value]) => {
194
- listItem.setAttribute(key, value)
195
- })
196
-
197
- return {
198
- dom: listItem,
199
- contentDOM: content,
200
- update: updatedNode => {
201
- if (updatedNode.type !== this.type) {
202
- return false
203
- }
204
-
205
- listItem.dataset.checked = updatedNode.attrs.checked
206
- if (updatedNode.attrs.checked) {
207
- checkbox.setAttribute('checked', 'checked')
208
- } else {
209
- checkbox.removeAttribute('checked')
210
- }
211
-
212
- return true
213
- },
214
- }
215
- }
216
- },
217
-
218
- addInputRules() {
219
- return [
220
- wrappingInputRule({
221
- find: inputRegex,
222
- type: this.type,
223
- getAttributes: match => ({
224
- checked: match[match.length - 1] === 'x',
225
- }),
226
- }),
227
- ]
228
- },
229
- })