prosemirror-suggestcat-plugin 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("prosemirror-state"),e=require("prosemirror-view"),n=require("fast-diff"),o=require("lodash.debounce");const r=new t.PluginKey("grammarSuggestPlugin"),i=t=>{let e="";return t.descendants(((t,n)=>{t.isText&&(e+=`${t.text}\n`)})),e},s=(t,e)=>{if(t===e)return{start:t.length,end:t.length,oldStart:t.length,oldEnd:t.length,oldText:"",newText:""};const o=n(t,e),r=o[0],i=r[0]===n.EQUAL?r[1].length-1:0,s=((t,e)=>{if(e[e.length-1][0]!==n.EQUAL)return t.length;const o=e.slice(0,e.length-1);let r=0;for(const t of o){const[e,o]=t;e===n.EQUAL&&(r+=o.length),e===n.DELETE&&(r+=o.length)}return r})(t,o),a=t.lastIndexOf("\n",i),c=t.indexOf("\n",s),l=-1===a?0:a+1,g=-1===c?t.length:c,p=t.slice(l,g),d=e.slice(l,e.length-(t.length-g));return{start:l,end:l+d.length,oldStart:l,oldEnd:g,oldText:p,newText:d}};var a,c;function l(t,e,n,o){return new(n||(n=Promise))((function(r,i){function s(t){try{c(o.next(t))}catch(t){i(t)}}function a(t){try{c(o.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}c((o=o.apply(t,e||[])).next())}))}!function(t){t.suggestionUpdate="suggestionUpdate",t.acceptSuggestion="acceptSuggestion",t.openSuggestion="openSuggestion",t.closeSuggestion="closeSuggestion",t.discardSuggestion="discardSuggestion"}(a||(a={})),function(t){t.grammarSuggestPopup="grammar-suggest-popup"}(c||(c={}));const g=[{docPos:1,textPos:0}],p=t=>{let e="";const n=[];return t.descendants(((t,o)=>{t.isText&&(n.push({docPos:o,textPos:e.length}),e+=`${t.text}\n`)})),{text:e,mapping:n.length?n:g}},d=(t,e)=>{for(let n=0;n<e.length;n++)if(t>=e[n].textPos&&(void 0===e[n+1]||t<e[n+1].textPos))return e[n].docPos+(t-e[n].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")},u=(t,e)=>l(void 0,void 0,void 0,(function*(){const n=[...e.split("\n")];return fetch("https://
|
|
1
|
+
"use strict";var t=require("prosemirror-state"),e=require("prosemirror-view"),n=require("fast-diff"),o=require("lodash.debounce");const r=new t.PluginKey("grammarSuggestPlugin"),i=t=>{let e="";return t.descendants(((t,n)=>{t.isText&&(e+=`${t.text}\n`)})),e},s=(t,e)=>{if(t===e)return{start:t.length,end:t.length,oldStart:t.length,oldEnd:t.length,oldText:"",newText:""};const o=n(t,e),r=o[0],i=r[0]===n.EQUAL?r[1].length-1:0,s=((t,e)=>{if(e[e.length-1][0]!==n.EQUAL)return t.length;const o=e.slice(0,e.length-1);let r=0;for(const t of o){const[e,o]=t;e===n.EQUAL&&(r+=o.length),e===n.DELETE&&(r+=o.length)}return r})(t,o),a=t.lastIndexOf("\n",i),c=t.indexOf("\n",s),l=-1===a?0:a+1,g=-1===c?t.length:c,p=t.slice(l,g),d=e.slice(l,e.length-(t.length-g));return{start:l,end:l+d.length,oldStart:l,oldEnd:g,oldText:p,newText:d}};var a,c;function l(t,e,n,o){return new(n||(n=Promise))((function(r,i){function s(t){try{c(o.next(t))}catch(t){i(t)}}function a(t){try{c(o.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}c((o=o.apply(t,e||[])).next())}))}!function(t){t.suggestionUpdate="suggestionUpdate",t.acceptSuggestion="acceptSuggestion",t.openSuggestion="openSuggestion",t.closeSuggestion="closeSuggestion",t.discardSuggestion="discardSuggestion"}(a||(a={})),function(t){t.grammarSuggestPopup="grammar-suggest-popup"}(c||(c={}));const g=[{docPos:1,textPos:0}],p=t=>{let e="";const n=[];return t.descendants(((t,o)=>{t.isText&&(n.push({docPos:o,textPos:e.length}),e+=`${t.text}\n`)})),{text:e,mapping:n.length?n:g}},d=(t,e)=>{for(let n=0;n<e.length;n++)if(t>=e[n].textPos&&(void 0===e[n+1]||t<e[n+1].textPos))return e[n].docPos+(t-e[n].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")},u=(t,e)=>l(void 0,void 0,void 0,(function*(){const n=[...e.split("\n")];return fetch("https://prosemirror-ai-plugin.web.app/api/suggestion",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:n}})}).then((t=>t.ok?t.json():Promise.reject(t))).then((t=>t&&t.some((t=>(t=>{try{JSON.parse(t)}catch(t){return!1}return!0})(t)))?{result:t.map((t=>JSON.parse(t).result)).join("\n"),fixed:!0}:{fixed:!1,result:e})).catch((t=>(t.text().then((e=>{console.error({status:t.status,text:e})})),{fixed:!1,result:e})))})),m=t=>t.replacement===t.original,f=t=>t.reduce(((t,e)=>{const n=t[t.length-1],o=t.slice(0,t.length-1);if(!n)return[e];return[...o,...((t,e)=>{if(t.to!==e.from)throw new Error(`Replace pairs must be adjacent\n\n, ${JSON.stringify({leftReplace:t,rightReplace:e})}`);if(t.replacement.endsWith("\n"))return[t,e];if(m(t)&&m(e))return[{from:t.from,to:e.to,original:t.original+e.original,replacement:t.replacement+e.replacement}];if(m(t)){if(e.replacement.startsWith(" ")&&""===e.original||e.original.startsWith(" ")&&""===e.replacement)return[t,e];const n=t.original.split(" "),o=n.pop()||"",r=n.join(" ")+(n.length?" ":"");return[...r.length?[{from:t.from,to:t.from+r.length,original:r,replacement:r}]:[],{from:t.from+r.length,to:e.to,original:o+e.original,replacement:o+e.replacement}]}if(m(e)){if(t.replacement.endsWith(" ")&&""===t.original||t.original.endsWith(" ")&&""===t.replacement)return[t,e];const n=e.original.split(" "),o=n.shift()||"",r=(n.length?" ":"")+n.join(" ");return" "===r?[{from:t.from,to:e.to,original:t.original+e.original,replacement:t.replacement+e.replacement}]:[{from:t.from,to:t.to+o.length,original:t.original+o,replacement:t.replacement+o},...r.length?[{from:t.to+o.length,to:e.to,original:r,replacement:r}]:[]]}return[{from:t.from,to:e.to,original:t.original+e.original,replacement:t.replacement+e.replacement}]})(n,e)]}),[]),h=(t,e)=>{const o=(t=>{let e=0;return t.map((([t,o])=>{switch(t){case n.EQUAL:const t=o.lastIndexOf("\n"),r={from:e,to:e+o.length,original:o,replacement:o};if(-1!==t&&"\n"!==o){const n=[{from:e,to:e+t+1,original:o.slice(0,t+1),replacement:o.slice(0,t+1)},{from:e+t+1,to:e+o.length,original:o.slice(t+1),replacement:o.slice(t+1)}];return e+=o.length,n}return e+=o.length,[r];case n.DELETE:const i={from:e,to:e+o.length,original:o,replacement:""};return e+=o.length,[i];case n.INSERT:return[{from:e,to:e,original:"",replacement:o}]}})).flat()})(n(t,e));return f(f(o))},x=(t,e)=>{var n;const o=null===(n=r.getState(t.state))||void 0===n?void 0:n.decorations.find(0,t.state.doc.nodeSize,(t=>t.id===e.spec.id))[0];if(!o)return;const{text:i}=o.spec,{from:s,to:c}=o,l={type:a.acceptSuggestion,id:e.spec.id},g=t.state.tr.insertText(i,s,c).setMeta(r,l);t.dispatch(g)},S=(t,e)=>{const{spec:n}=e,o={type:a.discardSuggestion,id:n.id},i=t.state.tr.setMeta(r,o);t.dispatch(i)},v=(t,e)=>{const n=r.getState(t.state);if(!n)return!1;const{decorations:o}=n,i=o.find(e,e)[0];if(!i){const e={type:a.closeSuggestion};return t.dispatch(t.state.tr.setMeta(r,e)),!1}const s=n.popupDecoration.find()[0];if((null==s?void 0:s.spec.id)===i.spec.id)return!1;const c={type:a.openSuggestion,decoration:i};return t.dispatch(t.state.tr.setMeta(r,c)),!1},y={debounceMs:2e3,createUpdatePopup:(t,e,n,o,r)=>{const i=document.createElement("div");i.className="grammar-suggest-tooltip";const{spec:s}=e,a=t.dom.getBoundingClientRect();i.id=c.grammarSuggestPopup;const l=t.coordsAtPos(n);i.style.left=l.left-a.left+"px",i.style.top=l.bottom-a.top+5+"px";const g=document.createElement("div");g.className="grammar-suggest-tooltip-apply",g.innerText=s.text,g.onclick=()=>{o(t,e)},i.appendChild(g);const p=document.createElement("div");return p.innerHTML="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM12 10.5858L14.8284 7.75736L16.2426 9.17157L13.4142 12L16.2426 14.8284L14.8284 16.2426L12 13.4142L9.17157 16.2426L7.75736 14.8284L10.5858 12L7.75736 9.17157L9.17157 7.75736L12 10.5858Z'></path></svg>",p.className="grammar-suggest-tooltip-discard",p.onclick=()=>{r(t,e)},i.appendChild(p),i}};exports.defaultOptions=y,exports.grammarSuggestPlugin=(n,c=y)=>{let l=!1;return new t.Plugin({key:r,state:{init:()=>({lastText:"",decorations:e.DecorationSet.empty,popupDecoration:e.DecorationSet.empty}),apply(t,n,o,l){const g=t.getMeta(r);return(null==g?void 0:g.type)===a.suggestionUpdate?((t,n,o)=>{const{changedRegion:r,fix:i,mapping:s,text:a}=n,c=h(r.newText,i.result).filter((t=>!m(t))).filter((t=>t.original!==`${t.replacement}\n`)).map((({from:t,to:n,original:o,replacement:i})=>{const a=d(r.start+t,s),c=d(r.start+(i.endsWith("\n")?n-1:n),s),l={text:i.endsWith("\n")?i.slice(0,-1):i,originalText:o,id:{}};return e.Decoration.inline(a,c,{class:"grammarSuggestion"+(""===o?"removalSuggestion":"")},l)}));return Object.assign(Object.assign({},t),{decorations:t.decorations.add(o.doc,c),lastText:a})})(n,g,t):(null==g?void 0:g.type)===a.acceptSuggestion?((t,n,o)=>{const r=t.decorations.remove(t.decorations.find(0,o.doc.nodeSize,(t=>t.id===n.id)));return Object.assign(Object.assign({},t),{lastText:i(o.doc),decorations:r.map(o.mapping,o.doc),popupDecoration:e.DecorationSet.empty})})(n,g,t):(null==g?void 0:g.type)===a.openSuggestion?((t,n,o,r)=>{const{decoration:i}=n,s={id:i.spec.id};return Object.assign(Object.assign({},t),{popupDecoration:e.DecorationSet.create(o.doc,[e.Decoration.widget(i.from,((t,e)=>{const n=e();return n?r.createUpdatePopup(t,i,n,x,S):document.createElement("div")}),Object.assign(Object.assign({},s),{stopEvent:()=>!0}))])})})(n,g,t,c):(null==g?void 0:g.type)===a.closeSuggestion?Object.assign(Object.assign({},n),{popupDecoration:e.DecorationSet.empty}):(null==g?void 0:g.type)===a.discardSuggestion?((t,n,o)=>Object.assign(Object.assign({},t),{decorations:t.decorations.remove(t.decorations.find(0,o.doc.nodeSize,(t=>t.id===n.id))),popupDecoration:e.DecorationSet.empty}))(n,g,t):t.docChanged?((t,e,n)=>{const o=p(n.doc).text,{text:r,mapping:i}=p(e.doc);if(r===o)return t;const a=s(o,r),c=t.decorations.map(e.mapping,e.doc),l=d(a.start,i),g=d(a.end,i),u=t.popupDecoration.map(e.mapping,e.doc);return Object.assign(Object.assign({},t),{decorations:c.remove(c.find(l,g)),popupDecoration:u.remove(u.find(l,g))})})(n,t,o):n}},props:{handleClick:v,decorations:t=>{const e=r.getState(t);return e?e.decorations.add(t.doc,e.popupDecoration.find()):null}},view(){const t=((t,e)=>o((t=>{var n;const o=p(t.state.doc),c=(null===(n=r.getState(t.state))||void 0===n?void 0:n.lastText)||"",l=s(c,o.text);u(e,l.newText).then((e=>{if(i(t.state.doc)!==o.text)return;const n={type:a.suggestionUpdate,fix:e,changedRegion:l,mapping:o.mapping,text:o.text};t.dispatch(t.state.tr.setMeta(r,n))})).catch((t=>{console.error("Grammar suggest API error",t)}))}),t.debounceMs))(c,n);return{update(e,n){const o=r.getState(e.state);e.state.doc.textBetween(0,e.state.doc.nodeSize-2,"/n")===n.doc.textBetween(0,n.doc.nodeSize-2,"/n")&&l||!o||o.lastText===i(e.state.doc)||(l=!0,t(e))}}}})};
|