@ctzhian/tiptap 1.5.7 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Editor/demo.js +42 -16
- package/dist/component/CustomDragHandle/index.js +0 -1
- package/dist/extension/component/Link/index.js +5 -10
- package/dist/extension/extension/AiWriting.d.ts +26 -0
- package/dist/extension/extension/AiWriting.js +344 -0
- package/dist/extension/extension/index.d.ts +1 -0
- package/dist/extension/extension/index.js +1 -0
- package/dist/extension/index.d.ts +1 -1
- package/dist/extension/index.js +21 -8
- package/dist/extension/node/FileHandler.d.ts +1 -1
- package/dist/extension/node/Link/index.js +1 -2
- package/dist/extension/node/Table.d.ts +1 -1
- package/dist/hook/index.d.ts +1 -1
- package/dist/hook/index.js +10 -2
- package/dist/index.css +8 -0
- package/dist/type/index.d.ts +3 -0
- package/package.json +1 -1
package/dist/Editor/demo.js
CHANGED
|
@@ -4,7 +4,7 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
4
4
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
5
|
import { Editor, EditorThemeProvider, EditorToolbar, useTiptap } from "./..";
|
|
6
6
|
import { Box } from '@mui/material';
|
|
7
|
-
import React from 'react';
|
|
7
|
+
import React, { useEffect } from 'react';
|
|
8
8
|
import { AiGenerate2Icon } from "../component/Icons";
|
|
9
9
|
import "../index.css";
|
|
10
10
|
var Reader = function Reader() {
|
|
@@ -15,36 +15,57 @@ var Reader = function Reader() {
|
|
|
15
15
|
console.log(editor.getHTML());
|
|
16
16
|
editor.commands.setContent(editor.getHTML());
|
|
17
17
|
},
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
var query;
|
|
18
|
+
onAiWritingGetSuggestion: function () {
|
|
19
|
+
var _onAiWritingGetSuggestion = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
20
|
+
var text;
|
|
22
21
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
23
22
|
while (1) switch (_context.prev = _context.next) {
|
|
24
23
|
case 0:
|
|
25
|
-
|
|
24
|
+
text = _ref.text;
|
|
26
25
|
return _context.abrupt("return", new Promise(function (resolve) {
|
|
26
|
+
resolve(['this is a default suggestion.', 'we are good.', 'what is your name?', 'how are you?', 'what is your favorite color?', 'what is your favorite food?', 'what is your favorite animal?', 'what is your favorite book?', 'what is your favorite movie?', 'what is your favorite song?', 'what is your favorite artist?', 'what is your favorite band?', 'what is your favorite city?', 'what is your favorite country?', 'what is your favorite sport?'][Math.floor(Math.random() * 10)]);
|
|
27
|
+
}));
|
|
28
|
+
case 2:
|
|
29
|
+
case "end":
|
|
30
|
+
return _context.stop();
|
|
31
|
+
}
|
|
32
|
+
}, _callee);
|
|
33
|
+
}));
|
|
34
|
+
function onAiWritingGetSuggestion(_x) {
|
|
35
|
+
return _onAiWritingGetSuggestion.apply(this, arguments);
|
|
36
|
+
}
|
|
37
|
+
return onAiWritingGetSuggestion;
|
|
38
|
+
}(),
|
|
39
|
+
// onTocUpdate: handleTocUpdate,
|
|
40
|
+
onMentionFilter: function () {
|
|
41
|
+
var _onMentionFilter = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
|
|
42
|
+
var query;
|
|
43
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
44
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
45
|
+
case 0:
|
|
46
|
+
query = _ref2.query;
|
|
47
|
+
return _context2.abrupt("return", new Promise(function (resolve) {
|
|
27
48
|
resolve(['Winona Ryder', 'Molly Ringwald', 'Ally Sheedy', 'Debbie Harry', 'Olivia Newton-John', 'Elton John', 'Michael J. Fox', 'Axl Rose', 'Emilio Estevez', 'Ralph Macchio', 'Rob Lowe', 'Jennifer Grey'].filter(function (item) {
|
|
28
49
|
return item.toLowerCase().startsWith(query.toLowerCase());
|
|
29
50
|
}).slice(0, 5));
|
|
30
51
|
}));
|
|
31
52
|
case 2:
|
|
32
53
|
case "end":
|
|
33
|
-
return
|
|
54
|
+
return _context2.stop();
|
|
34
55
|
}
|
|
35
|
-
},
|
|
56
|
+
}, _callee2);
|
|
36
57
|
}));
|
|
37
|
-
function onMentionFilter(
|
|
58
|
+
function onMentionFilter(_x2) {
|
|
38
59
|
return _onMentionFilter.apply(this, arguments);
|
|
39
60
|
}
|
|
40
61
|
return onMentionFilter;
|
|
41
62
|
}(),
|
|
42
63
|
onUpload: function () {
|
|
43
|
-
var _onUpload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
44
|
-
return _regeneratorRuntime().wrap(function
|
|
45
|
-
while (1) switch (
|
|
64
|
+
var _onUpload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(file, onProgress) {
|
|
65
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
66
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
46
67
|
case 0:
|
|
47
|
-
return
|
|
68
|
+
return _context3.abrupt("return", new Promise(function (resolve) {
|
|
48
69
|
var progress = 0;
|
|
49
70
|
var interval = setInterval(function () {
|
|
50
71
|
progress += Math.random() * 20;
|
|
@@ -72,11 +93,11 @@ var Reader = function Reader() {
|
|
|
72
93
|
}));
|
|
73
94
|
case 1:
|
|
74
95
|
case "end":
|
|
75
|
-
return
|
|
96
|
+
return _context3.stop();
|
|
76
97
|
}
|
|
77
|
-
},
|
|
98
|
+
}, _callee3);
|
|
78
99
|
}));
|
|
79
|
-
function onUpload(
|
|
100
|
+
function onUpload(_x3, _x4) {
|
|
80
101
|
return _onUpload.apply(this, arguments);
|
|
81
102
|
}
|
|
82
103
|
return onUpload;
|
|
@@ -84,6 +105,11 @@ var Reader = function Reader() {
|
|
|
84
105
|
content: ""
|
|
85
106
|
}),
|
|
86
107
|
editor = _useTiptap.editor;
|
|
108
|
+
useEffect(function () {
|
|
109
|
+
if (editor) {
|
|
110
|
+
editor.commands.setAiWriting(true);
|
|
111
|
+
}
|
|
112
|
+
}, [editor]);
|
|
87
113
|
return /*#__PURE__*/React.createElement(EditorThemeProvider, {
|
|
88
114
|
mode: "light"
|
|
89
115
|
}, /*#__PURE__*/React.createElement(Box, {
|
|
@@ -258,7 +258,6 @@ var CustomDragHandle = function CustomDragHandle(_ref3) {
|
|
|
258
258
|
};
|
|
259
259
|
var updateNodeChange = useCallback(function (data) {
|
|
260
260
|
var _data$node, _current$node2;
|
|
261
|
-
console.log(data);
|
|
262
261
|
if ((data.pos !== current.pos || ((_data$node = data.node) === null || _data$node === void 0 ? void 0 : _data$node.type.name) !== ((_current$node2 = current.node) === null || _current$node2 === void 0 ? void 0 : _current$node2.type.name)) && data.pos !== -1 || data.node === null && data.pos >= 0) {
|
|
263
262
|
var _data$node2;
|
|
264
263
|
var allResources = data.node ? getAllResources(data.node) : [];
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
3
2
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
4
3
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
5
4
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
@@ -145,13 +144,11 @@ var LinkViewWrapper = function LinkViewWrapper(_ref) {
|
|
|
145
144
|
editor: editor
|
|
146
145
|
});
|
|
147
146
|
}
|
|
148
|
-
return /*#__PURE__*/React.createElement(NodeViewWrapper,
|
|
149
|
-
className: "link-wrapper ".concat(attrs.class, " ").concat(attrs.type === 'block' ? 'block-link-wrapper' : '', " ").concat(selected ? 'ProseMirror-selectednode' : '')
|
|
150
|
-
|
|
151
|
-
'data-drag-handle': true
|
|
152
|
-
} : {}, {
|
|
147
|
+
return /*#__PURE__*/React.createElement(NodeViewWrapper, {
|
|
148
|
+
className: "link-wrapper ".concat(attrs.class, " ").concat(attrs.type === 'block' ? 'block-link-wrapper' : '', " ").concat(selected ? 'ProseMirror-selectednode' : ''),
|
|
149
|
+
"data-drag-handle": true,
|
|
153
150
|
as: attrs.type === 'block' ? 'div' : 'span'
|
|
154
|
-
}
|
|
151
|
+
}, attrs.type === 'block' ? /*#__PURE__*/React.createElement(Stack, {
|
|
155
152
|
direction: 'row',
|
|
156
153
|
alignItems: 'center',
|
|
157
154
|
gap: 2,
|
|
@@ -174,8 +171,7 @@ var LinkViewWrapper = function LinkViewWrapper(_ref) {
|
|
|
174
171
|
alignSelf: 'center',
|
|
175
172
|
bgcolor: '#FFFFFF'
|
|
176
173
|
},
|
|
177
|
-
src: favicon
|
|
178
|
-
component: "span"
|
|
174
|
+
src: favicon
|
|
179
175
|
}, /*#__PURE__*/React.createElement(LinkIcon, {
|
|
180
176
|
sx: {
|
|
181
177
|
fontSize: '2rem',
|
|
@@ -223,7 +219,6 @@ var LinkViewWrapper = function LinkViewWrapper(_ref) {
|
|
|
223
219
|
fontWeight: 500
|
|
224
220
|
}
|
|
225
221
|
}, attrs.type === 'icon' && /*#__PURE__*/React.createElement(Avatar, {
|
|
226
|
-
component: "span",
|
|
227
222
|
sx: {
|
|
228
223
|
width: '1rem',
|
|
229
224
|
height: '1rem',
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core';
|
|
2
|
+
import { DecorationSet } from '@tiptap/pm/view';
|
|
3
|
+
export interface AiWritingOptions {
|
|
4
|
+
minChars?: number;
|
|
5
|
+
debounceMs?: number;
|
|
6
|
+
}
|
|
7
|
+
declare module '@tiptap/core' {
|
|
8
|
+
interface Commands<ReturnType> {
|
|
9
|
+
aiWriting: {
|
|
10
|
+
setAiWriting: (enabled: boolean) => ReturnType;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
type SuggestionState = {
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
pos: number | null;
|
|
17
|
+
text: string;
|
|
18
|
+
decorations: DecorationSet;
|
|
19
|
+
lastDocText: string;
|
|
20
|
+
};
|
|
21
|
+
export declare const AiWritingExtension: (props: {
|
|
22
|
+
onAiWritingGetSuggestion?: (({ text }: {
|
|
23
|
+
text: string;
|
|
24
|
+
}) => Promise<string>) | undefined;
|
|
25
|
+
}) => Extension<AiWritingOptions, any>;
|
|
26
|
+
export type { SuggestionState };
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
7
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
8
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
9
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
10
|
+
import { Extension } from '@tiptap/core';
|
|
11
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
12
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
13
|
+
var aiWritingPluginKey = new PluginKey('aiWritingPlugin');
|
|
14
|
+
|
|
15
|
+
// 简单防抖
|
|
16
|
+
function debounce(fn, wait) {
|
|
17
|
+
var timer = null;
|
|
18
|
+
return function () {
|
|
19
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
20
|
+
args[_key] = arguments[_key];
|
|
21
|
+
}
|
|
22
|
+
if (timer) clearTimeout(timer);
|
|
23
|
+
timer = setTimeout(function () {
|
|
24
|
+
return fn.apply(void 0, args);
|
|
25
|
+
}, wait);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function getFullText(view) {
|
|
29
|
+
var doc = view.state.doc;
|
|
30
|
+
return doc.textBetween(0, doc.content.size, '\n', '\n');
|
|
31
|
+
}
|
|
32
|
+
function isAtEndWithNoContentAfter(view) {
|
|
33
|
+
var state = view.state;
|
|
34
|
+
var selection = state.selection,
|
|
35
|
+
doc = state.doc;
|
|
36
|
+
if (!selection.empty) return false;
|
|
37
|
+
// 文档需非空
|
|
38
|
+
var fullText = getFullText(view);
|
|
39
|
+
if (!fullText || fullText.trim().length === 0) return false;
|
|
40
|
+
// 光标后文档范围内无可见字符
|
|
41
|
+
var afterText = doc.textBetween(selection.from, doc.content.size, '\n', '\n');
|
|
42
|
+
if (afterText && afterText.trim().length > 0) return false;
|
|
43
|
+
// 同时在当前文本块末尾(不在块中间)
|
|
44
|
+
var $from = selection.$from;
|
|
45
|
+
var atEndOfBlock = $from.parentOffset === $from.parent.content.size;
|
|
46
|
+
return !!atEndOfBlock;
|
|
47
|
+
}
|
|
48
|
+
function createSuggestionWidget(text) {
|
|
49
|
+
var dom = document.createElement('span');
|
|
50
|
+
dom.className = 'ai-writing-suggestion';
|
|
51
|
+
dom.textContent = text;
|
|
52
|
+
return dom;
|
|
53
|
+
}
|
|
54
|
+
export var AiWritingExtension = function AiWritingExtension(props) {
|
|
55
|
+
return Extension.create({
|
|
56
|
+
name: 'aiWriting',
|
|
57
|
+
addOptions: function addOptions() {
|
|
58
|
+
return {
|
|
59
|
+
minChars: 1,
|
|
60
|
+
debounceMs: 1000
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
addStorage: function addStorage() {
|
|
64
|
+
return {
|
|
65
|
+
enabled: false
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
addCommands: function addCommands() {
|
|
69
|
+
var _this = this;
|
|
70
|
+
return {
|
|
71
|
+
setAiWriting: function setAiWriting(enabled) {
|
|
72
|
+
return function (_ref) {
|
|
73
|
+
var tr = _ref.tr,
|
|
74
|
+
state = _ref.state,
|
|
75
|
+
dispatch = _ref.dispatch;
|
|
76
|
+
if (dispatch) {
|
|
77
|
+
var meta = {
|
|
78
|
+
type: 'setEnabled',
|
|
79
|
+
enabled: enabled
|
|
80
|
+
};
|
|
81
|
+
dispatch(tr.setMeta(aiWritingPluginKey, meta))
|
|
82
|
+
// 同步到 storage,便于外部判断
|
|
83
|
+
;
|
|
84
|
+
_this.storage.enabled = !!enabled;
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
addKeyboardShortcuts: function addKeyboardShortcuts() {
|
|
92
|
+
var _this2 = this;
|
|
93
|
+
return {
|
|
94
|
+
Tab: function Tab() {
|
|
95
|
+
var pluginState = aiWritingPluginKey.getState(_this2.editor.state);
|
|
96
|
+
if (!pluginState || !pluginState.enabled) return false;
|
|
97
|
+
var text = pluginState.text,
|
|
98
|
+
pos = pluginState.pos;
|
|
99
|
+
if (!text || text.length === 0 || pos == null) return false;
|
|
100
|
+
return _this2.editor.commands.command(function (_ref2) {
|
|
101
|
+
var tr = _ref2.tr,
|
|
102
|
+
state = _ref2.state,
|
|
103
|
+
dispatch = _ref2.dispatch;
|
|
104
|
+
if (!dispatch) return true;
|
|
105
|
+
var insertPos = state.selection.from;
|
|
106
|
+
tr.insertText(text, insertPos);
|
|
107
|
+
// 接受后清空建议
|
|
108
|
+
dispatch(tr.setMeta(aiWritingPluginKey, {
|
|
109
|
+
type: 'clearSuggestion'
|
|
110
|
+
}));
|
|
111
|
+
return true;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
addProseMirrorPlugins: function addProseMirrorPlugins() {
|
|
117
|
+
var _this$options$minChar, _this$options$debounc;
|
|
118
|
+
var minChars = (_this$options$minChar = this.options.minChars) !== null && _this$options$minChar !== void 0 ? _this$options$minChar : 1;
|
|
119
|
+
var debounceMs = (_this$options$debounc = this.options.debounceMs) !== null && _this$options$debounc !== void 0 ? _this$options$debounc : 1000;
|
|
120
|
+
var debouncedRequest = null;
|
|
121
|
+
var request = /*#__PURE__*/function () {
|
|
122
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(view) {
|
|
123
|
+
var state, text, suggestion, _yield$props$onAiWrit, _props$onAiWritingGet, tr;
|
|
124
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
125
|
+
while (1) switch (_context.prev = _context.next) {
|
|
126
|
+
case 0:
|
|
127
|
+
state = aiWritingPluginKey.getState(view.state);
|
|
128
|
+
if (!(!state || !state.enabled)) {
|
|
129
|
+
_context.next = 3;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
return _context.abrupt("return");
|
|
133
|
+
case 3:
|
|
134
|
+
if (isAtEndWithNoContentAfter(view)) {
|
|
135
|
+
_context.next = 5;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
return _context.abrupt("return");
|
|
139
|
+
case 5:
|
|
140
|
+
text = getFullText(view);
|
|
141
|
+
if (!(((text === null || text === void 0 ? void 0 : text.length) || 0) < minChars)) {
|
|
142
|
+
_context.next = 8;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
return _context.abrupt("return");
|
|
146
|
+
case 8:
|
|
147
|
+
if (!(state.lastDocText === text)) {
|
|
148
|
+
_context.next = 10;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
return _context.abrupt("return");
|
|
152
|
+
case 10:
|
|
153
|
+
suggestion = '';
|
|
154
|
+
_context.prev = 11;
|
|
155
|
+
_context.next = 14;
|
|
156
|
+
return (_props$onAiWritingGet = props.onAiWritingGetSuggestion) === null || _props$onAiWritingGet === void 0 ? void 0 : _props$onAiWritingGet.call(props, {
|
|
157
|
+
text: text
|
|
158
|
+
});
|
|
159
|
+
case 14:
|
|
160
|
+
_context.t1 = _yield$props$onAiWrit = _context.sent;
|
|
161
|
+
_context.t0 = _context.t1 !== null;
|
|
162
|
+
if (!_context.t0) {
|
|
163
|
+
_context.next = 18;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
_context.t0 = _yield$props$onAiWrit !== void 0;
|
|
167
|
+
case 18:
|
|
168
|
+
if (!_context.t0) {
|
|
169
|
+
_context.next = 22;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
_context.t2 = _yield$props$onAiWrit;
|
|
173
|
+
_context.next = 23;
|
|
174
|
+
break;
|
|
175
|
+
case 22:
|
|
176
|
+
_context.t2 = '';
|
|
177
|
+
case 23:
|
|
178
|
+
suggestion = _context.t2;
|
|
179
|
+
_context.next = 29;
|
|
180
|
+
break;
|
|
181
|
+
case 26:
|
|
182
|
+
_context.prev = 26;
|
|
183
|
+
_context.t3 = _context["catch"](11);
|
|
184
|
+
console.error('getSuggestion error', _context.t3);
|
|
185
|
+
case 29:
|
|
186
|
+
tr = view.state.tr.setMeta(aiWritingPluginKey, {
|
|
187
|
+
type: 'setSuggestion',
|
|
188
|
+
text: suggestion,
|
|
189
|
+
pos: view.state.selection.from,
|
|
190
|
+
lastDocText: text
|
|
191
|
+
});
|
|
192
|
+
view.dispatch(tr);
|
|
193
|
+
case 31:
|
|
194
|
+
case "end":
|
|
195
|
+
return _context.stop();
|
|
196
|
+
}
|
|
197
|
+
}, _callee, null, [[11, 26]]);
|
|
198
|
+
}));
|
|
199
|
+
return function request(_x) {
|
|
200
|
+
return _ref3.apply(this, arguments);
|
|
201
|
+
};
|
|
202
|
+
}();
|
|
203
|
+
var ensureDebounced = function ensureDebounced() {
|
|
204
|
+
if (!debouncedRequest) {
|
|
205
|
+
debouncedRequest = debounce(function (view) {
|
|
206
|
+
return request(view);
|
|
207
|
+
}, debounceMs);
|
|
208
|
+
}
|
|
209
|
+
return debouncedRequest;
|
|
210
|
+
};
|
|
211
|
+
return [new Plugin({
|
|
212
|
+
key: aiWritingPluginKey,
|
|
213
|
+
state: {
|
|
214
|
+
init: function init(_config, state) {
|
|
215
|
+
return {
|
|
216
|
+
enabled: false,
|
|
217
|
+
pos: null,
|
|
218
|
+
text: '',
|
|
219
|
+
decorations: DecorationSet.create(state.doc, []),
|
|
220
|
+
lastDocText: ''
|
|
221
|
+
};
|
|
222
|
+
},
|
|
223
|
+
apply: function apply(tr, pluginState, _old, newState) {
|
|
224
|
+
var next = pluginState;
|
|
225
|
+
|
|
226
|
+
// 装饰位置随映射移动
|
|
227
|
+
if (tr.docChanged) {
|
|
228
|
+
var mapped = pluginState.decorations.map(tr.mapping, tr.doc);
|
|
229
|
+
next = _objectSpread(_objectSpread({}, next), {}, {
|
|
230
|
+
decorations: mapped
|
|
231
|
+
});
|
|
232
|
+
// 用户输入则清空建议(除非是我们主动 setSuggestion 立即覆盖)
|
|
233
|
+
if (!tr.getMeta(aiWritingPluginKey)) {
|
|
234
|
+
next = _objectSpread(_objectSpread({}, next), {}, {
|
|
235
|
+
text: '',
|
|
236
|
+
pos: null,
|
|
237
|
+
decorations: DecorationSet.create(newState.doc, [])
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
var meta = tr.getMeta(aiWritingPluginKey);
|
|
242
|
+
if (meta) {
|
|
243
|
+
switch (meta.type) {
|
|
244
|
+
case 'setEnabled':
|
|
245
|
+
{
|
|
246
|
+
var _enabled = !!meta.enabled;
|
|
247
|
+
// 关闭时清空
|
|
248
|
+
var cleared = _enabled ? next.decorations : DecorationSet.create(newState.doc, []);
|
|
249
|
+
return _objectSpread(_objectSpread({}, next), {}, {
|
|
250
|
+
enabled: _enabled,
|
|
251
|
+
text: _enabled ? next.text : '',
|
|
252
|
+
pos: _enabled ? next.pos : null,
|
|
253
|
+
decorations: cleared
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
case 'setSuggestion':
|
|
257
|
+
{
|
|
258
|
+
var _meta$lastDocText;
|
|
259
|
+
var text = meta.text || '';
|
|
260
|
+
var pos = typeof meta.pos === 'number' ? meta.pos : null;
|
|
261
|
+
var lastDocText = (_meta$lastDocText = meta.lastDocText) !== null && _meta$lastDocText !== void 0 ? _meta$lastDocText : next.lastDocText;
|
|
262
|
+
if (!text || pos == null) {
|
|
263
|
+
return _objectSpread(_objectSpread({}, next), {}, {
|
|
264
|
+
text: '',
|
|
265
|
+
pos: null,
|
|
266
|
+
decorations: DecorationSet.create(newState.doc, []),
|
|
267
|
+
lastDocText: lastDocText
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
var deco = Decoration.widget(pos, function () {
|
|
271
|
+
return createSuggestionWidget(text);
|
|
272
|
+
}, {
|
|
273
|
+
side: 1,
|
|
274
|
+
ignoreSelection: true
|
|
275
|
+
});
|
|
276
|
+
var decoSet = DecorationSet.create(newState.doc, [deco]);
|
|
277
|
+
return _objectSpread(_objectSpread({}, next), {}, {
|
|
278
|
+
text: text,
|
|
279
|
+
pos: pos,
|
|
280
|
+
decorations: decoSet,
|
|
281
|
+
lastDocText: lastDocText
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
case 'clearSuggestion':
|
|
285
|
+
{
|
|
286
|
+
return _objectSpread(_objectSpread({}, next), {}, {
|
|
287
|
+
text: '',
|
|
288
|
+
pos: null,
|
|
289
|
+
decorations: DecorationSet.create(newState.doc, [])
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return next;
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
view: function (_view) {
|
|
298
|
+
function view(_x2) {
|
|
299
|
+
return _view.apply(this, arguments);
|
|
300
|
+
}
|
|
301
|
+
view.toString = function () {
|
|
302
|
+
return _view.toString();
|
|
303
|
+
};
|
|
304
|
+
return view;
|
|
305
|
+
}(function (view) {
|
|
306
|
+
// 每次视图更新时尝试触发请求(防抖)
|
|
307
|
+
var handler = function handler() {
|
|
308
|
+
var state = aiWritingPluginKey.getState(view.state);
|
|
309
|
+
if (!state || !state.enabled) return;
|
|
310
|
+
if (!isAtEndWithNoContentAfter(view)) return;
|
|
311
|
+
ensureDebounced()(view);
|
|
312
|
+
};
|
|
313
|
+
handler();
|
|
314
|
+
return {
|
|
315
|
+
update: handler,
|
|
316
|
+
destroy: function destroy() {
|
|
317
|
+
// noop
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}),
|
|
321
|
+
props: {
|
|
322
|
+
decorations: function decorations(state) {
|
|
323
|
+
var _aiWritingPluginKey$g;
|
|
324
|
+
return ((_aiWritingPluginKey$g = aiWritingPluginKey.getState(state)) === null || _aiWritingPluginKey$g === void 0 ? void 0 : _aiWritingPluginKey$g.decorations) || null;
|
|
325
|
+
},
|
|
326
|
+
// 用户任意输入时隐藏建议(例如组合键输入不一定触发 docChanged 前)
|
|
327
|
+
handleKeyDown: function handleKeyDown(view, event) {
|
|
328
|
+
var ps = aiWritingPluginKey.getState(view.state);
|
|
329
|
+
if (!ps || !ps.enabled) return false;
|
|
330
|
+
// Tab 的处理放在 addKeyboardShortcuts
|
|
331
|
+
if (event.key === 'Tab') return false;
|
|
332
|
+
if (ps.text) {
|
|
333
|
+
// 任何按键都清空(如箭头、字符、Enter 等),让体验更贴近代码编辑器
|
|
334
|
+
view.dispatch(view.state.tr.setMeta(aiWritingPluginKey, {
|
|
335
|
+
type: 'clearSuggestion'
|
|
336
|
+
}));
|
|
337
|
+
}
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
})];
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { GetExtensionsProps } from '../type';
|
|
2
|
-
export declare const getExtensions: ({ limit, exclude, extensions: extensionsProps, youtube, editable, mentionItems, onMentionFilter, onUpload, onError, onTocUpdate, }: GetExtensionsProps) => any;
|
|
2
|
+
export declare const getExtensions: ({ limit, exclude, extensions: extensionsProps, youtube, editable, mentionItems, onMentionFilter, onUpload, onError, onTocUpdate, onAiWritingGetSuggestion, }: GetExtensionsProps) => any;
|
package/dist/extension/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import { TextStyleKit } from '@tiptap/extension-text-style';
|
|
|
13
13
|
import { CharacterCount, Placeholder } from '@tiptap/extensions';
|
|
14
14
|
import StarterKit from '@tiptap/starter-kit';
|
|
15
15
|
import { PLACEHOLDER } from "../contants/placeholder";
|
|
16
|
-
import { SlashCommands, StructuredDiffExtension } from "./extension";
|
|
16
|
+
import { AiWritingExtension, SlashCommands, StructuredDiffExtension } from "./extension";
|
|
17
17
|
import { CodeExtension } from "./mark/Code";
|
|
18
18
|
import { AlertExtension, AudioExtension, BlockAttachmentExtension, BlockLinkExtension, CodeBlockLowlightExtension, CustomBlockMathExtension, CustomInlineMathExtension, DetailsContentExtension, DetailsExtension, DetailsSummaryExtension, EmojiExtension, FileHandlerExtension, IframeExtension, ImageExtension, Indent, InlineAttachmentExtension, InlineLinkExtension, ListExtension, MentionExtension, TableExtension, TableOfContents, UploadProgressExtension, VerticalAlign, VideoExtension, YoutubeExtension } from "./node";
|
|
19
19
|
export var getExtensions = function getExtensions(_ref) {
|
|
@@ -26,7 +26,8 @@ export var getExtensions = function getExtensions(_ref) {
|
|
|
26
26
|
onMentionFilter = _ref.onMentionFilter,
|
|
27
27
|
onUpload = _ref.onUpload,
|
|
28
28
|
onError = _ref.onError,
|
|
29
|
-
onTocUpdate = _ref.onTocUpdate
|
|
29
|
+
onTocUpdate = _ref.onTocUpdate,
|
|
30
|
+
onAiWritingGetSuggestion = _ref.onAiWritingGetSuggestion;
|
|
30
31
|
var defaultExtensions = [StarterKit.configure({
|
|
31
32
|
link: false,
|
|
32
33
|
code: false,
|
|
@@ -63,18 +64,23 @@ export var getExtensions = function getExtensions(_ref) {
|
|
|
63
64
|
emptyNodeClass: 'custom-placeholder-node',
|
|
64
65
|
showOnlyWhenEditable: true,
|
|
65
66
|
placeholder: function placeholder(_ref2) {
|
|
66
|
-
var
|
|
67
|
+
var _storage;
|
|
68
|
+
var editor = _ref2.editor,
|
|
69
|
+
node = _ref2.node,
|
|
67
70
|
pos = _ref2.pos;
|
|
68
71
|
var type = node.type,
|
|
69
72
|
attrs = node.attrs;
|
|
70
73
|
if (pos === 0) {
|
|
71
74
|
return PLACEHOLDER.default;
|
|
72
75
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
var aiWritingEnabled = !!(editor !== null && editor !== void 0 && (_storage = editor.storage) !== null && _storage !== void 0 && (_storage = _storage.aiWriting) !== null && _storage !== void 0 && _storage.enabled);
|
|
77
|
+
if (!aiWritingEnabled) {
|
|
78
|
+
if (type.name === 'heading') {
|
|
79
|
+
return PLACEHOLDER.heading[attrs.level];
|
|
80
|
+
}
|
|
81
|
+
if (PLACEHOLDER[type.name]) {
|
|
82
|
+
return PLACEHOLDER[type.name];
|
|
83
|
+
}
|
|
78
84
|
}
|
|
79
85
|
return '';
|
|
80
86
|
}
|
|
@@ -120,6 +126,7 @@ export var getExtensions = function getExtensions(_ref) {
|
|
|
120
126
|
defaultExtensions.push(Youtube);
|
|
121
127
|
}
|
|
122
128
|
if (editable) {
|
|
129
|
+
// 编辑模式
|
|
123
130
|
if (!(exclude !== null && exclude !== void 0 && exclude.includes('fileHandler'))) {
|
|
124
131
|
var FileHandler = FileHandlerExtension({
|
|
125
132
|
onUpload: onUpload
|
|
@@ -131,10 +138,16 @@ export var getExtensions = function getExtensions(_ref) {
|
|
|
131
138
|
if (!(exclude !== null && exclude !== void 0 && exclude.includes('slashCommands'))) {
|
|
132
139
|
defaultExtensions.push(SlashCommands);
|
|
133
140
|
}
|
|
141
|
+
if (!(exclude !== null && exclude !== void 0 && exclude.includes('aiWriting'))) {
|
|
142
|
+
defaultExtensions.push(AiWritingExtension({
|
|
143
|
+
onAiWritingGetSuggestion: onAiWritingGetSuggestion
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
134
146
|
if (!(exclude !== null && exclude !== void 0 && exclude.includes('invisibleCharacters'))) {
|
|
135
147
|
defaultExtensions.push(InvisibleCharacters);
|
|
136
148
|
}
|
|
137
149
|
} else {
|
|
150
|
+
// 只读模式
|
|
138
151
|
if (!(exclude !== null && exclude !== void 0 && exclude.includes('structuredDiff'))) {
|
|
139
152
|
defaultExtensions.push(StructuredDiffExtension);
|
|
140
153
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { UploadFunction } from "../../type";
|
|
2
2
|
export declare const FileHandlerExtension: (props: {
|
|
3
3
|
onUpload?: UploadFunction;
|
|
4
|
-
}) => import("@tiptap/core").Extension<Omit<import("@tiptap/extension-file-handler").FileHandlePluginOptions, "
|
|
4
|
+
}) => import("@tiptap/core").Extension<Omit<import("@tiptap/extension-file-handler").FileHandlePluginOptions, "editor" | "key">, any>;
|
|
@@ -37,8 +37,7 @@ export var InlineLinkExtension = Node.create({
|
|
|
37
37
|
group: 'inline',
|
|
38
38
|
inline: true,
|
|
39
39
|
atom: true,
|
|
40
|
-
|
|
41
|
-
draggable: false,
|
|
40
|
+
draggable: true,
|
|
42
41
|
selectable: true,
|
|
43
42
|
onCreate: function onCreate() {
|
|
44
43
|
if (this.options.validate && !this.options.shouldAutoLink) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Extension } from '@tiptap/core';
|
|
2
2
|
export declare const TableExtension: ({ editable }: {
|
|
3
3
|
editable: boolean;
|
|
4
|
-
}) => (import("@tiptap/core").Node<import("@tiptap/extension-table").TableOptions, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableHeaderOptions, any>
|
|
4
|
+
}) => (Extension<any, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableOptions, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableHeaderOptions, any>)[];
|
|
5
5
|
export default TableExtension;
|
package/dist/hook/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { UseTiptapProps, UseTiptapReturn } from "../type";
|
|
2
2
|
import { UseEditorOptions } from '@tiptap/react';
|
|
3
|
-
declare const useTiptap: ({ exclude, extensions: extensionsProps, mentionItems, onMentionFilter, onSave, onError, onUpload, onTocUpdate, editable, ...options }: UseTiptapProps & UseEditorOptions) => UseTiptapReturn;
|
|
3
|
+
declare const useTiptap: ({ exclude, extensions: extensionsProps, mentionItems, onMentionFilter, onSave, onError, onUpload, onTocUpdate, onAiWritingGetSuggestion, editable, ...options }: UseTiptapProps & UseEditorOptions) => UseTiptapReturn;
|
|
4
4
|
export default useTiptap;
|
package/dist/hook/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
var _excluded = ["exclude", "extensions", "mentionItems", "onMentionFilter", "onSave", "onError", "onUpload", "onTocUpdate", "editable"];
|
|
2
|
+
var _excluded = ["exclude", "extensions", "mentionItems", "onMentionFilter", "onSave", "onError", "onUpload", "onTocUpdate", "onAiWritingGetSuggestion", "editable"];
|
|
3
3
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
4
4
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
5
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -20,6 +20,7 @@ var useTiptap = function useTiptap(_ref) {
|
|
|
20
20
|
onError = _ref.onError,
|
|
21
21
|
onUpload = _ref.onUpload,
|
|
22
22
|
onTocUpdate = _ref.onTocUpdate,
|
|
23
|
+
onAiWritingGetSuggestion = _ref.onAiWritingGetSuggestion,
|
|
23
24
|
_ref$editable = _ref.editable,
|
|
24
25
|
editable = _ref$editable === void 0 ? true : _ref$editable,
|
|
25
26
|
options = _objectWithoutProperties(_ref, _excluded);
|
|
@@ -31,7 +32,8 @@ var useTiptap = function useTiptap(_ref) {
|
|
|
31
32
|
onMentionFilter: onMentionFilter,
|
|
32
33
|
onUpload: onUpload,
|
|
33
34
|
onError: onError,
|
|
34
|
-
onTocUpdate: onTocUpdate
|
|
35
|
+
onTocUpdate: onTocUpdate,
|
|
36
|
+
onAiWritingGetSuggestion: onAiWritingGetSuggestion
|
|
35
37
|
});
|
|
36
38
|
var editor = useEditor(_objectSpread(_objectSpread({
|
|
37
39
|
editable: editable,
|
|
@@ -47,6 +49,12 @@ var useTiptap = function useTiptap(_ref) {
|
|
|
47
49
|
}
|
|
48
50
|
// tab
|
|
49
51
|
if (event.key === 'Tab') {
|
|
52
|
+
var _storage;
|
|
53
|
+
// 若开启了 aiWriting,则放行给扩展处理(Tab 接受建议),不再插入制表符
|
|
54
|
+
var aiWritingEnabled = !!(editor !== null && editor !== void 0 && (_storage = editor.storage) !== null && _storage !== void 0 && (_storage = _storage.aiWriting) !== null && _storage !== void 0 && _storage.enabled);
|
|
55
|
+
if (aiWritingEnabled) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
50
58
|
var isInList = (editor === null || editor === void 0 ? void 0 : editor.isActive('orderedList')) || (editor === null || editor === void 0 ? void 0 : editor.isActive('bulletList')) || (editor === null || editor === void 0 ? void 0 : editor.isActive('taskList'));
|
|
51
59
|
var isInTable = editor === null || editor === void 0 ? void 0 : editor.isActive('table');
|
|
52
60
|
if (!isInList && !isInTable) {
|
package/dist/index.css
CHANGED
|
@@ -552,4 +552,12 @@
|
|
|
552
552
|
padding-left: 0.25rem;
|
|
553
553
|
font-size: 14px;
|
|
554
554
|
line-height: 24px;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/* AI 续写建议样式 */
|
|
558
|
+
.tiptap.ProseMirror .ai-writing-suggestion {
|
|
559
|
+
color: var(--mui-palette-text-disabled);
|
|
560
|
+
pointer-events: none;
|
|
561
|
+
user-select: none;
|
|
562
|
+
white-space: pre-wrap;
|
|
555
563
|
}
|
package/dist/type/index.d.ts
CHANGED
|
@@ -70,6 +70,9 @@ export type EditorFnProps = {
|
|
|
70
70
|
onError?: (error: Error) => void;
|
|
71
71
|
onUpload?: UploadFunction;
|
|
72
72
|
onTocUpdate?: (toc: TocList) => void;
|
|
73
|
+
onAiWritingGetSuggestion?: ({ text }: {
|
|
74
|
+
text: string;
|
|
75
|
+
}) => Promise<string>;
|
|
73
76
|
};
|
|
74
77
|
export type MentionItems = string[];
|
|
75
78
|
export type MentionExtensionProps = {
|