prosemirror-suggestcat-plugin 0.0.3 → 0.0.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/README.md +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("prosemirror-state"),t=require("prosemirror-view"),n=require("fast-diff"),o=require("lodash.debounce");const r=new e.PluginKey("grammarSuggestPlugin"),i=e=>{let t="";return e.descendants(((e,n)=>{e.isText&&(t+=`${e.text}\n`)})),t},
|
|
1
|
+
"use strict";var e=require("prosemirror-state"),t=require("prosemirror-view"),n=require("fast-diff"),o=require("lodash.debounce");const r=new e.PluginKey("grammarSuggestPlugin"),i=e=>{let t="";return e.descendants(((e,n)=>{e.isText&&(t+=`${e.text}\n`)})),t},s=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const o=n(e,t),r=o[0],i=r[0]===n.EQUAL?r[1].length-1:0,s=((e,t)=>{if(t[t.length-1][0]!==n.EQUAL)return e.length;const o=t.slice(0,t.length-1);let r=0;for(const e of o){const[t,o]=e;t===n.EQUAL&&(r+=o.length),t===n.DELETE&&(r+=o.length)}return r})(e,o),a=e.lastIndexOf("\n",i),c=e.indexOf("\n",s),l=-1===a?0:a+1,g=-1===c?e.length:c,p=e.slice(l,g),d=t.slice(l,t.length-(e.length-g));return{start:l,end:l+d.length,oldStart:l,oldEnd:g,oldText:p,newText:d}};var a,c;function l(e,t,n,o){return new(n||(n=Promise))((function(r,i){function s(e){try{c(o.next(e))}catch(e){i(e)}}function a(e){try{c(o.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}!function(e){e.suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion"}(a||(a={})),function(e){e.grammarSuggestPopup="grammar-suggest-popup"}(c||(c={}));const g=[{docPos:1,textPos:0}],p=e=>{let t="";const n=[];return e.descendants(((e,o)=>{e.isText&&(n.push({docPos:o,textPos:t.length}),t+=`${e.text}\n`)})),{text:t,mapping:n.length?n:g}},d=(e,t)=>{for(let n=0;n<t.length;n++)if(e>=t[n].textPos&&(void 0===t[n+1]||e<t[n+1].textPos))return t[n].docPos+(e-t[n].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")},u=(e,t)=>l(void 0,void 0,void 0,(function*(){const n=[...t.split("\n")];return fetch("https://openairequest-gw5lxik4dq-uc.a.run.app",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:n}})}).then((e=>e.json())).then((e=>e&&e.some((e=>(e=>{try{JSON.parse(e)}catch(e){return!1}return!0})(e)))?{result:e.map((e=>JSON.parse(e).result)).join("\n"),fixed:!0}:{fixed:!1,result:t})).catch((e=>(console.error(e.message),{fixed:!1,result:t})))})),m=e=>e.replacement===e.original,f=e=>e.reduce(((e,t)=>{const n=e[e.length-1],o=e.slice(0,e.length-1);if(!n)return[t];return[...o,...((e,t)=>{if(e.to!==t.from)throw new Error(`Replace pairs must be adjacent\n\n, ${JSON.stringify({leftReplace:e,rightReplace:t})}`);if(e.replacement.endsWith("\n"))return[e,t];if(m(e)&&m(t))return[{from:e.from,to:t.to,original:e.original+t.original,replacement:e.replacement+t.replacement}];if(m(e)){if(t.replacement.startsWith(" ")&&""===t.original||t.original.startsWith(" ")&&""===t.replacement)return[e,t];const n=e.original.split(" "),o=n.pop()||"",r=n.join(" ")+(n.length?" ":"");return[...r.length?[{from:e.from,to:e.from+r.length,original:r,replacement:r}]:[],{from:e.from+r.length,to:t.to,original:o+t.original,replacement:o+t.replacement}]}if(m(t)){if(e.replacement.endsWith(" ")&&""===e.original||e.original.endsWith(" ")&&""===e.replacement)return[e,t];const n=t.original.split(" "),o=n.shift()||"",r=(n.length?" ":"")+n.join(" ");return" "===r?[{from:e.from,to:t.to,original:e.original+t.original,replacement:e.replacement+t.replacement}]:[{from:e.from,to:e.to+o.length,original:e.original+o,replacement:e.replacement+o},...r.length?[{from:e.to+o.length,to:t.to,original:r,replacement:r}]:[]]}return[{from:e.from,to:t.to,original:e.original+t.original,replacement:e.replacement+t.replacement}]})(n,t)]}),[]),h=(e,t)=>{const o=(e=>{let t=0;return e.map((([e,o])=>{switch(e){case n.EQUAL:const e=o.lastIndexOf("\n"),r={from:t,to:t+o.length,original:o,replacement:o};if(-1!==e&&"\n"!==o){const n=[{from:t,to:t+e+1,original:o.slice(0,e+1),replacement:o.slice(0,e+1)},{from:t+e+1,to:t+o.length,original:o.slice(e+1),replacement:o.slice(e+1)}];return t+=o.length,n}return t+=o.length,[r];case n.DELETE:const i={from:t,to:t+o.length,original:o,replacement:""};return t+=o.length,[i];case n.INSERT:return[{from:t,to:t,original:"",replacement:o}]}})).flat()})(n(e,t));return f(f(o))},x=(e,t)=>{var n;const o=null===(n=r.getState(e.state))||void 0===n?void 0:n.decorations.find(0,e.state.doc.nodeSize,(e=>e.id===t.spec.id))[0];if(!o)return;const{text:i}=o.spec,{from:s,to:c}=o,l={type:a.acceptSuggestion,id:t.spec.id},g=e.state.tr.insertText(i,s,c).setMeta(r,l);e.dispatch(g)},S=(e,t)=>{const{spec:n}=t,o={type:a.discardSuggestion,id:n.id},i=e.state.tr.setMeta(r,o);e.dispatch(i)},v=(e,t)=>{const n=r.getState(e.state);if(!n)return!1;const{decorations:o}=n,i=o.find(t,t)[0];if(!i){const t={type:a.closeSuggestion};return e.dispatch(e.state.tr.setMeta(r,t)),!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 e.dispatch(e.state.tr.setMeta(r,c)),!1},y={debounceMs:2e3,createUpdatePopup:(e,t,n,o,r)=>{const i=document.createElement("div");i.className="grammar-suggest-tooltip";const{spec:s}=t,a=e.dom.getBoundingClientRect();i.id=c.grammarSuggestPopup;const l=e.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(e,t)},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(e,t)},i.appendChild(p),i}};exports.defaultOptions=y,exports.grammarSuggestPlugin=(n,c=y)=>{console.log(Object.assign({apiKey:n},c));let l=!1;return new e.Plugin({key:r,state:{init:()=>({lastText:"",decorations:t.DecorationSet.empty,popupDecoration:t.DecorationSet.empty}),apply(e,n,o,l){const g=e.getMeta(r);return(null==g?void 0:g.type)===a.suggestionUpdate?((e,n,o)=>{const{changedRegion:r,fix:i,mapping:s,text:a}=n,c=h(r.newText,i.result).filter((e=>!m(e))).filter((e=>e.original!==`${e.replacement}\n`)).map((({from:e,to:n,original:o,replacement:i})=>{const a=d(r.start+e,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 t.Decoration.inline(a,c,{class:"grammarSuggestion"+(""===o?"removalSuggestion":"")},l)}));return Object.assign(Object.assign({},e),{decorations:e.decorations.add(o.doc,c),lastText:a})})(n,g,e):(null==g?void 0:g.type)===a.acceptSuggestion?((e,n,o)=>{const r=e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===n.id)));return Object.assign(Object.assign({},e),{lastText:i(o.doc),decorations:r.map(o.mapping,o.doc),popupDecoration:t.DecorationSet.empty})})(n,g,e):(null==g?void 0:g.type)===a.openSuggestion?((e,n,o,r)=>{const{decoration:i}=n,s={id:i.spec.id};return Object.assign(Object.assign({},e),{popupDecoration:t.DecorationSet.create(o.doc,[t.Decoration.widget(i.from,((e,t)=>{const n=t();return n?r.createUpdatePopup(e,i,n,x,S):document.createElement("div")}),Object.assign(Object.assign({},s),{stopEvent:()=>!0}))])})})(n,g,e,c):(null==g?void 0:g.type)===a.closeSuggestion?Object.assign(Object.assign({},n),{popupDecoration:t.DecorationSet.empty}):(null==g?void 0:g.type)===a.discardSuggestion?((e,n,o)=>Object.assign(Object.assign({},e),{decorations:e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===n.id))),popupDecoration:t.DecorationSet.empty}))(n,g,e):e.docChanged?((e,t,n)=>{const o=p(n.doc).text,{text:r,mapping:i}=p(t.doc);if(r===o)return e;const a=s(o,r),c=e.decorations.map(t.mapping,t.doc),l=d(a.start,i),g=d(a.end,i),u=e.popupDecoration.map(t.mapping,t.doc);return Object.assign(Object.assign({},e),{decorations:c.remove(c.find(l,g)),popupDecoration:u.remove(u.find(l,g))})})(n,e,o):n}},props:{handleClick:v,decorations:e=>{const t=r.getState(e);return t?t.decorations.add(e.doc,t.popupDecoration.find()):null}},view(){const e=((e,t)=>o((e=>{var n;const o=p(e.state.doc),c=(null===(n=r.getState(e.state))||void 0===n?void 0:n.lastText)||"",l=s(c,o.text);u(t,l.newText).then((t=>{if(i(e.state.doc)!==o.text)return;const n={type:a.suggestionUpdate,fix:t,changedRegion:l,mapping:o.mapping,text:o.text};e.dispatch(e.state.tr.setMeta(r,n))})).catch((e=>{console.error("Grammar suggest API error",e)}))}),e.debounceMs))(c,n);return{update(t,n){const o=r.getState(t.state);t.state.doc.textBetween(0,t.state.doc.nodeSize-2,"/n")===n.doc.textBetween(0,n.doc.nodeSize-2,"/n")&&l||!o||o.lastText===i(t.state.doc)||(l=!0,e(t))}}}})};
|