@veltdev/slate-velt-comments-staging 1.0.2 → 1.0.4
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/cjs/index.js +1 -1
- package/cjs/index.js.map +1 -1
- package/cjs/types/velt.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/types/velt.d.ts +1 -1
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t=require("slate"),e=require("slate-react"),o=require("react");function r(t,e,o,r){return new(o||(o=Promise))((function(n,i){function c(t){try{s(r.next(t))}catch(t){i(t)}}function a(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(c,a)}s((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const n={EDITOR_ID:"data-editor-id",LOCATION_ID:"data-velt-location-id"},i={VELT_COMMENT:"veltComment"};var c;class a{}c=a,a.selectedCommentsMap=new Map,a.subscribers=new Map,a.isSubscribed=!1,a.getCommentElement=()=>{try{const t=window.Velt;return(null==t?void 0:t.getCommentElement)?t.getCommentElement():null}catch(t){return c.catchError("Error getting comment element:",t),null}},a.addManualComment=({context:t,location:e})=>{try{const o=c.getCommentElement();return o?o.addManualComment({context:t,location:e}):Promise.resolve(null)}catch(t){return c.catchError("Error adding manual comment:",t),Promise.resolve(null)}},a.updateContext=({annotationId:t,context:e})=>{try{const o=c.getCommentElement();return o?o.updateContext(t,e):Promise.resolve(null)}catch(t){c.catchError("Error updating context:",t)}},a.subscribeToSelectedCommentsMap=(t,e)=>{try{c.subscribeToSelectedAnnotationsMapSingleton(),c.subscribers.set(t,e)}catch(t){c.catchError("Error subscribing to selected comments map:",t)}},a.unsubscribeFromSelectedCommentsMap=t=>{try{c.subscribers.delete(t)}catch(t){c.catchError("Error unsubscribing from selected comments map:",t)}},a.catchError=(t,e)=>{var o;try{let r={};e&&"object"==typeof e&&(r=Object.assign(Object.assign({},r),e)),t&&(r.message=t),r.sdkVersion=null===(o=window.Velt)||void 0===o?void 0:o.version}catch(t){}},a.subscribeToSelectedAnnotationsMapSingleton=()=>{var t;try{if(c.isSubscribed)return;const e=c.getCommentElement();e&&"function"==typeof(null==e?void 0:e.getSelectedComments)&&(null===(t=null==e?void 0:e.getSelectedComments())||void 0===t||t.subscribe((t=>{const e=new Map;null==t||t.forEach((t=>{(null==t?void 0:t.annotationId)&&e.set(null==t?void 0:t.annotationId,!0)}));((t,e)=>{if(t.size!==e.size)return!1;for(const[o,r]of t)if(e.get(o)!==r)return!1;return!0})(e,c.selectedCommentsMap||new Map)||c.updateSelectedCommentsMap(e)})),c.isSubscribed=!0)}catch(t){return new Map}},a.updateSelectedCommentsMap=t=>{try{c.selectedCommentsMap=t,c.subscribers.forEach((t=>{"function"==typeof t&&t(c.selectedCommentsMap)}))}catch(t){c.catchError("Error updating selected comments map:",t)}};class s{constructor(o){this.getSelectedText=e=>{try{return e.selection?t.Editor.string(e,e.selection):null}catch(t){return a.catchError("Error getting selected text:",t),null}},this.findOccurrenceOfText=t=>{try{const{searchText:e,scopeElement:o,startOffset:r=0,caseSensitive:n=!0}=t;if(!e)return{total:0,position:0};const i="string"==typeof o?o:(null==o?void 0:o.textContent)||"",c=n?t=>t:t=>t.toLowerCase(),a=c(i),s=c(e),l=[];let d=0;for(;;){const t=a.indexOf(s,d);if(-1===t)break;l.push(t),d=t+s.length}const u=l.filter((t=>t<r)).length+1;return{total:l.length,position:u}}catch(t){return a.catchError("Error finding occurrence of text:",t),null}},this.findOccurrenceOfSelectedText=(t,o)=>{var r;try{const n=this.getSelectedText(t);if(!n)return 0;if(!o){let e=0;const o=this.findAllMatches(t,n),r=t.selection;if(r){const t=o.findIndex((t=>t.anchor.path.toString()===r.anchor.path.toString()&&t.focus.path.toString()===r.focus.path.toString()&&t.anchor.offset===r.anchor.offset&&t.focus.offset===r.focus.offset));-1!==t&&(e=t+1)}return e}const i=e.ReactEditor.toDOMNode(t,t),c=null===(r=t.selection)||void 0===r?void 0:r.anchor.offset,a=this.findOccurrenceOfText({searchText:n,scopeElement:o||i,startOffset:c});return null==a?void 0:a.position}catch(t){return a.catchError("Error finding occurrence of selected text:",t),null}},this.findAllMatches=(e,o,r={caseSensitive:!0})=>{try{const{caseSensitive:n=!0}=r,i=[];if(!o)return i;const c=t=>"object"==typeof t&&null!==t&&"text"in t?t.text:"",s=t=>"object"==typeof t&&null!==t&&"text"in t&&"string"==typeof t.text,l=(t,e)=>{if(s(t)){const r=c(t),a=n?o:o.toLowerCase(),s=n?r:r.toLowerCase();let l=0;for(;;){const t=s.indexOf(a,l);if(-1===t)break;const r={anchor:{path:e,offset:t},focus:{path:e,offset:t+o.length}};i.push(r),l=t+1}}"children"in t&&Array.isArray(t.children)&&t.children.forEach(((t,o)=>{l(t,[...e,o])}))};try{const[o]=t.Editor.nodes(e,{at:[],mode:"highest"});o&&l(o[0],o[1])}catch(t){a.catchError("Error searching in document:",t)}return i}catch(t){return a.catchError("Error finding all matches:",t),[]}},this.findParentNodeWithIdForSelection=e=>{try{const{selection:o}=e;if(!o)return null;const{anchor:r}=o,n=t.Node.get(e,r.path);return this.findParentNodeWithId(e,{node:n,path:r.path})}catch(t){return a.catchError("Error finding parent node with ID for selection:",t),null}},this.findParentNodeWithId=(o,r)=>{var n;try{let{node:i,path:c}=r||{};if(!i)return null;c&&(i=t.Editor.node(o,c)[0]);let a=null,s=null;try{a=e.ReactEditor.toDOMNode(o,i)}catch(t){return null}try{s=e.ReactEditor.toDOMNode(o,o)}catch(t){return null}const l=null!==(n=null==r?void 0:r.maxIterations)&&void 0!==n?n:100,d=null==r?void 0:r.idPattern;let u=a,h=0;for(;u&&u!==document.body&&h<l;){if(!s.contains(u))return null;if(u.id&&(!d||d.test(u.id)))return u;u=u.parentElement,h++}return null}catch(t){return a.catchError("Error finding parent node with ID:",t),null}},this.removeVeltCommentFromEditor=(o,r)=>{try{if(!o||!r)throw new Error("Invalid input: editor and annotationId are required");const n=[];for(const[e,i]of t.Node.descendants(o))s.isVeltCommentText(e)&&e.annotationId===r&&n.push(i);try{n.reverse().forEach((r=>{const n=t.Editor.node(o,r)[0],i=e.ReactEditor.toDOMNode(o,n);if(null==i?void 0:i.isConnected){const e=()=>{t.Transforms.unwrapNodes(o,{at:r,mode:"lowest"})},n=s.getHistoryEditor();n&&"function"==typeof n.withoutSaving?n.withoutSaving(o,(()=>{e()})):e()}}))}catch(t){a.catchError("Error unwrapping elements:",t)}return!0}catch(t){return a.catchError("Error removing Velt comment:",t),!1}},this.highlightTextWithVeltComment=(t,e)=>{var o;try{const{text:r,targetTextNodeId:n,occurrence:i}=(null===(o=null==e?void 0:e.context)||void 0===o?void 0:o.textEditorConfig)||{};if(!r||!i)return;this.injectTag(t,r,i,e.annotationId,n)&&e.annotationId&&this.plugin.storeService.annotationStore.set(e.annotationId,{annotation:e})}catch(t){a.catchError("Error highlighting text with velt comment:",t)}},this.getEditorTextContent=e=>{try{return e.children.map((e=>t.Node.string(e))).join("")}catch(t){return a.catchError("Error getting editor text content:",t),""}},this.onSlateValueChange=({editor:e})=>{try{const o=this.collectDocumentAnnotations({editor:e});if(0===o.size)return;const r=new Map;for(const[n]of o){if(!this.plugin.storeService.annotationStore.get(n))continue;if(!Array.from(t.Editor.nodes(e,{at:[]})).filter((([t])=>s.isVeltCommentText(t))).filter((([t])=>t.annotationId===n)).map((([o,r])=>({node:o,pos:t.Editor.start(e,r).offset}))).length)continue;const o=this.processAnnotationChanges({editor:e,annotationId:n});o&&r.set(n,o)}r.size>0&&this.updateVeltComments({changes:r,editor:e})}catch(t){a.catchError("Error in processVeltCommentChanges:",t)}},this.injectTag=(o,r,n,c,l)=>{try{let a=null;if(l){if(a=document.getElementById(l),!a)return!1;if(!this.isElementInEditor(o,a))return!1}else a=e.ReactEditor.toDOMNode(o,o);if(!a)return!1;const d=this.findTextOccurrence(o,r,n,a);if(!d)return!1;if(Array.from(t.Editor.nodes(o,{at:d,match:t=>s.isVeltCommentText(t)})).length>0)return!1;const u=()=>{o.wrapNodes({type:i.VELT_COMMENT,children:[],annotationId:c},{at:d,split:!0})},h=s.getHistoryEditor();return h&&"function"==typeof h.withoutSaving?h.withoutSaving(o,(()=>{u()})):u(),!0}catch(t){return a.catchError("Error injecting tag:",t),!1}},this.plugin=o}collectDocumentAnnotations({editor:e}){try{const o=new Map;return Array.from(t.Editor.nodes(e,{at:[]})).filter((([t])=>s.isVeltCommentText(t))).forEach((([t])=>{const e=t.annotationId;if(e){const t=this.plugin.storeService.annotationStore.get(e);(null==t?void 0:t.annotation)&&o.set(e,t.annotation)}})),o}catch(t){return a.catchError("Error collecting document annotations:",t),new Map}}processAnnotationChanges({editor:o,annotationId:r}){var n,i,c,l;try{let a=null,d=null,u=null;const h=null===(n=this.plugin.storeService.annotationStore.get(r))||void 0===n?void 0:n.annotation;if(!(null===(i=null==h?void 0:h.context)||void 0===i?void 0:i.textEditorConfig))return null;const{text:f,targetTextNodeId:m,occurrence:g}=(null===(c=null==h?void 0:h.context)||void 0===c?void 0:c.textEditorConfig)||{},E=Array.from(t.Editor.nodes(o,{at:[],match:t=>s.isVeltCommentText(t)&&t.annotationId===r,mode:"lowest"}));for(const[t,e]of E)a=t,u=e;if(!a)return null;try{if(d=e.ReactEditor.toDOMNode(o,a),!d)return null}catch(t){return null}const v=E.map((([e])=>t.Node.string(e))).join("");if(!v)return null;const p=this.findParentNodeWithId(o,{node:a,path:null!=u?u:void 0});let x,C,S;p&&(x=p.id),p&&(C=e.ReactEditor.toSlateNode(o,p),S=e.ReactEditor.findPath(o,C));const T=null===(l=this.findTextOccurrenceOfNode(o,a,null!=u?u:void 0,C,S))||void 0===l?void 0:l.nodeOccurrencePosition;if(!T)return null;const y=v!==f,b=T!==g,I=m!==x;return y||b||I?{contentChanged:y,currentText:v,occurrenceChanged:b,currentOccurrence:null!=T?T:0,originalOccurrence:g,annotationId:r,originalText:f,originalTargetTextNodeId:m,newTargetTextNodeId:x,annotation:h,multiThreadAnnotationId:null==h?void 0:h.multiThreadAnnotationId,targetTextNodeIdChanged:I}:null}catch(t){return a.catchError("Error processing annotation changes:",t),null}}updateVeltComments({changes:t,editor:e}){var o;try{for(const[r,n]of Array.from(t.entries()))try{if(!(null===(o=n.annotation.context)||void 0===o?void 0:o.textEditorConfig))continue;const t=JSON.parse(JSON.stringify(n.annotation.context.textEditorConfig));n.contentChanged&&(t.text=n.currentText),n.occurrenceChanged&&(t.occurrence=n.currentOccurrence),n.targetTextNodeIdChanged&&(t.targetTextNodeId=n.newTargetTextNodeId);const i=Object.assign(Object.assign({},n.annotation.context),{textEditorConfig:t});this.plugin.storeService.annotationStore.remove(r),this.removeVeltCommentFromEditor(e,r),a.updateContext({annotationId:r,context:i})}catch(t){a.catchError("Error updating Velt comments",t)}}catch(t){a.catchError("Error updating Velt comments",t)}}isElementInEditor(t,o){try{return e.ReactEditor.toDOMNode(t,t).contains(o)}catch(t){return a.catchError("Error checking element in editor:",t),!1}}traverseNodesForText(e,o,r,n){try{if(t.Text.isText(e)){const t=n.length,i=(n+=e.text).length;r.push({node:e,path:o,start:t,end:i})}else if(t.Element.isElement(e))for(let t=0;t<e.children.length;t++){const i=[...o,t];n=this.traverseNodesForText(e.children[t],i,r,n)}return n}catch(t){return a.catchError("Error traversing nodes for text:",t),""}}collectSubtreeText(t,e,o){try{const r=[];let n="";if(0===o.length)for(let e=0;e<t.children.length;e++){const o=[e];n=this.traverseNodesForText(t.children[e],o,r,n)}else n=this.traverseNodesForText(e,o,r,n);return{fullText:n,textNodePositions:r}}catch(t){return a.catchError("Error collecting subtree text:",t),{fullText:"",textNodePositions:[]}}}findTextOccurrence(t,o,r,n){try{const i=e.ReactEditor.toSlateNode(t,n),c=e.ReactEditor.findPath(t,i),{fullText:a,textNodePositions:s}=this.collectSubtreeText(t,i,c),l=[];let d;const u=new RegExp(o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g");for(;null!==(d=u.exec(a));)l.push({index:d.index});if(l.length<r)return null;const h=l[r-1].index,f=h+o.length;let m,g;for(const t of s)if(h>=t.start&&h<t.end&&(m=t),f>t.start&&f<=t.end&&(g=t),m&&g)return{anchor:{path:m.path,offset:h-m.start},focus:{path:g.path,offset:f-g.start}};return null}catch(t){return a.catchError("Error in findTextOccurrence:",t),null}}findAllTextOccurrences(e,o,r=!0,n,i){try{if(!o)return[];const c=[],s=r?t=>t:t=>t.toLowerCase(),l=s(o);if(n&&!i)try{for(const[o,r]of t.Editor.nodes(e))if(o===n){i=r;break}}catch(t){a.catchError("Error finding container path:",t)}const d=i||[];for(const[r,n]of t.Editor.nodes(e,{at:d,match:e=>t.Text.isText(e)})){if(i&&!t.Path.isDescendant(n,i)&&!t.Path.equals(n,i))continue;const e=t.Node.string(r),a=s(e);let d,u=0;for(;-1!==(d=a.indexOf(l,u));){const t={path:n,offset:d},i={path:n,offset:d+o.length},a={anchor:t,focus:i};c.push({node:r,path:n,range:a,startPoint:t,endPoint:i,text:e.substring(d,d+o.length)}),u=d+l.length}}return c}catch(t){return a.catchError("Error finding all text occurrences:",t),[]}}findTextOccurrenceOfNode(o,r,n,i,c){try{const s=t.Node.string(r);if(!s.trim())return{node:r,nodePath:n,text:s,totalOccurrences:0,nodeOccurrencePosition:0,textOccurrences:[],containerNode:i,containerPath:c};if(!n)try{if("findPath"in e.ReactEditor)try{n=e.ReactEditor.findPath(o,r)}catch(t){a.catchError("Error finding path with ReactEditor:",t)}if(!n)for(const[e,i]of t.Editor.nodes(o))if(e===r){n=i;break}}catch(t){a.catchError("Error finding node path:",t)}const l=this.findAllTextOccurrences(o,s,!0,i,c);let d=0;if(n)for(let e=0;e<l.length;e++){const o=l[e],r=t.Path.equals(o.path,n),i=t.Path.isAncestor(n,o.path);if(r||i){d=e+1;break}}return{node:r,nodePath:n,text:s,totalOccurrences:l.length,nodeOccurrencePosition:d,textOccurrences:l,containerNode:i,containerPath:c}}catch(t){return a.catchError("Error finding text occurrence of node:",t),null}}}s.setHistoryEditor=t=>{s.HistoryEditor=t},s.getHistoryEditor=()=>s.HistoryEditor,s.getEditorId=t=>{var o,r;try{const i=e.ReactEditor.toDOMNode(t,t);return i&&(null!==(r=null===(o=null==i?void 0:i.closest(`[${n.EDITOR_ID}]`))||void 0===o?void 0:o.getAttribute(n.EDITOR_ID))&&void 0!==r)?r:null}catch(t){return a.catchError("Error finding editor ID:",t),null}},s.isVeltCommentText=t=>t.type===i.VELT_COMMENT;class l{constructor(t){this.isSubscribed=!1,this.globalStore={editor:null,comments:[],filteredComments:[],selectedCommentsMap:new Map},this.globalAnnotationStore=new Map,this.annotationStore={get:t=>this.globalAnnotationStore.get(t),set:(t,e)=>{this.globalAnnotationStore.set(t,e)},getAll:()=>new Map(this.globalAnnotationStore),clear:()=>{this.globalAnnotationStore.clear()},remove:t=>{this.globalAnnotationStore.delete(t)}},this.updateGlobalStore=t=>{var e,o,r;try{const n=(null==t?void 0:t.comments)||(null===(e=this.globalStore)||void 0===e?void 0:e.comments)||[],i=(null==t?void 0:t.selectedCommentsMap)||(null===(o=this.globalStore)||void 0===o?void 0:o.selectedCommentsMap)||new Map,c=(null===(r=this.globalStore)||void 0===r?void 0:r.filteredComments)||[],a=n.filter((t=>{var e;return"terminal"!==(null===(e=null==t?void 0:t.status)||void 0===e?void 0:e.type)||(null==i?void 0:i.get(null==t?void 0:t.annotationId))})),s=new Set(a.map((t=>null==t?void 0:t.annotationId)));c.map((t=>null==t?void 0:t.annotationId)).filter((t=>t&&!s.has(t))).forEach((t=>{var e,o;(null===(e=this.globalStore)||void 0===e?void 0:e.editor)&&t&&this.plugin.editorService.removeVeltCommentFromEditor(null===(o=this.globalStore)||void 0===o?void 0:o.editor,t)})),this.globalStore=Object.assign(Object.assign(Object.assign({},this.globalStore),t),{filteredComments:a}),this.highlightCommentsFromGlobalStore()}catch(t){a.catchError("Error updating global store:",t)}},this.highlightCommentsFromGlobalStore=()=>{var t,e;try{const{editor:o,filteredComments:r}=this.globalStore;if(!o||!r)return;for(const n of r){(null===(e=null===(t=null==n?void 0:n.context)||void 0===t?void 0:t.textEditorConfig)||void 0===e?void 0:e.text)&&this.plugin.editorService.highlightTextWithVeltComment(o,n)}}catch(t){a.catchError("Error highlighting comments from global store:",t)}},this.plugin=t;try{a.subscribeToSelectedCommentsMap(this.plugin.editorId,(t=>{try{this.updateGlobalStore({selectedCommentsMap:t})}catch(t){a.catchError("Error updating global store:",t)}}))}catch(t){a.catchError("Error subscribing to selected comments map:",t)}}}l.storeInstanceMap=new Map;const d=new Map;class u{constructor(t,e){this.previousEditorTextContent="",this.editorId=t,this.editor=e,this.editorService=new s(this),this.storeService=new l(this),e&&this.setPreviousEditorTextContent(this.editorService.getEditorTextContent(e))}static getInstance(t){let{editorId:e,editor:o}=t||{};return e||(e="default"),d.has(e)||d.set(e,new u(e,o)),d.get(e)}getPreviousEditorTextContent(){return this.previousEditorTextContent}setPreviousEditorTextContent(t){this.previousEditorTextContent=t}}const h=t=>r(void 0,[t],void 0,(function*({editorId:t,editor:e,context:o}){var r,n;try{if(!a.getCommentElement())return;t||(t=null!==(r=s.getEditorId(e))&&void 0!==r?r:void 0);const i=u.getInstance({editorId:t,editor:e}),c=i.editorService.getSelectedText(e);if(!c)return;const l=i.editorService.findParentNodeWithIdForSelection(e);let d,h=null;l&&(d=l.id,h=null===(n=l.closest("[data-velt-location-id]"))||void 0===n?void 0:n.getAttribute("data-velt-location-id"));const f=i.editorService.findOccurrenceOfSelectedText(e,null!=l?l:void 0);if(!f)return;let m,g={};o&&"object"==typeof o&&(g=Object.assign(Object.assign({},g),o)),g.textEditorConfig={text:c,occurrence:f,targetTextNodeId:d},t&&(g.textEditorConfig.editorId=t),yield new Promise((t=>setTimeout(t,200))),h&&(m={id:h});try{a.addManualComment({context:g,location:m})}catch(t){a.catchError("Error creating comment",t)}}catch(t){a.catchError("Error adding comment",t)}}));const f=({editor:t,editorId:e,commentAnnotations:o})=>{var r;try{if(t){e||(e=null!==(r=s.getEditorId(t))&&void 0!==r?r:void 0),o=null==o?void 0:o.filter((t=>{var o,r,n;return!!(null===(o=t.context)||void 0===o?void 0:o.textEditorConfig)&&(null===(n=null===(r=t.context)||void 0===r?void 0:r.textEditorConfig)||void 0===n?void 0:n.editorId)===e}));u.getInstance({editorId:e,editor:t}).storeService.updateGlobalStore({editor:t,comments:o})}}catch(t){a.catchError("Error highlighting tiptap-velt-comments",t)}},m=({editor:t,editorId:e})=>{var o;try{e||(e=null!==(o=s.getEditorId(t))&&void 0!==o?o:void 0);const r=u.getInstance({editorId:e,editor:t}),n=r.editorService.getEditorTextContent(t);if(n===r.getPreviousEditorTextContent())return;r.setPreviousEditorTextContent(n),r.editorService.onSlateValueChange({editor:t})}catch(t){a.catchError("Error in processVeltCommentChanges:",t)}};exports.SlateVeltComment=t=>{const{attributes:e,children:r,element:n}=t,i=null==n?void 0:n.annotationId;return o.createElement("velt-comment-text",Object.assign({"annotation-id":i},e),r)},exports.addComment=h,exports.highlightComments=function(t,e,o){try{f({editor:t,editorId:o,commentAnnotations:e})}catch(t){a.catchError("Error highlighting tiptap-velt-comments",t)}},exports.onSlateValueChange=m,exports.renderComments=f,exports.triggerAddComment=(t,e)=>r(void 0,void 0,void 0,(function*(){try{return h({editor:t,context:null==e?void 0:e.context})}catch(t){a.catchError("Error triggering add comment",t)}})),exports.withVeltComments=(e,o)=>{const{isInline:r,isVoid:n,onChange:i}=e;return(null==o?void 0:o.HistoryEditor)&&s.setHistoryEditor(o.HistoryEditor),e.isInline=t=>!!s.isVeltCommentText(t)||r(t),e.isVoid=t=>!s.isVeltCommentText(t)&&n(t),e.onChange=()=>{i&&i();e.operations.some((e=>t.Operation.isTextOperation(e)))&&m({editor:e})},e};
|
|
1
|
+
"use strict";var t=require("slate"),e=require("slate-react"),o=require("react");function r(t,e,o,r){return new(o||(o=Promise))((function(n,i){function c(t){try{s(r.next(t))}catch(t){i(t)}}function a(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(c,a)}s((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const n={EDITOR_ID:"data-editor-id",LOCATION_ID:"data-velt-location-id"},i={VELT_COMMENT:"veltComment"};var c;class a{}c=a,a.selectedCommentsMap=new Map,a.subscribers=new Map,a.isSubscribed=!1,a.getCommentElement=()=>{try{const t=window.Velt;return(null==t?void 0:t.getCommentElement)?t.getCommentElement():null}catch(t){return c.catchError("Error getting comment element:",t),null}},a.addManualComment=({context:t,location:e})=>{try{const o=c.getCommentElement();return o?o.addManualComment({context:t,location:e}):Promise.resolve(null)}catch(t){return c.catchError("Error adding manual comment:",t),Promise.resolve(null)}},a.updateContext=({annotationId:t,context:e})=>{try{const o=c.getCommentElement();return o?o.updateContext(t,e):Promise.resolve(null)}catch(t){c.catchError("Error updating context:",t)}},a.subscribeToSelectedCommentsMap=(t,e)=>{try{c.subscribeToSelectedAnnotationsMapSingleton(),c.subscribers.set(t,e)}catch(t){c.catchError("Error subscribing to selected comments map:",t)}},a.unsubscribeFromSelectedCommentsMap=t=>{try{c.subscribers.delete(t)}catch(t){c.catchError("Error unsubscribing from selected comments map:",t)}},a.catchError=(t,e)=>{var o;try{let r={};e&&"object"==typeof e&&(r=Object.assign(Object.assign({},r),e)),t&&(r.message=t),r.sdkVersion=null===(o=window.Velt)||void 0===o?void 0:o.version}catch(t){}},a.subscribeToSelectedAnnotationsMapSingleton=()=>{var t;try{if(c.isSubscribed)return;const e=c.getCommentElement();e&&"function"==typeof(null==e?void 0:e.getSelectedComments)&&(null===(t=null==e?void 0:e.getSelectedComments())||void 0===t||t.subscribe((t=>{const e=new Map;null==t||t.forEach((t=>{(null==t?void 0:t.annotationId)&&e.set(null==t?void 0:t.annotationId,!0)}));((t,e)=>{if(t.size!==e.size)return!1;for(const[o,r]of t)if(e.get(o)!==r)return!1;return!0})(e,c.selectedCommentsMap||new Map)||c.updateSelectedCommentsMap(e)})),c.isSubscribed=!0)}catch(t){return new Map}},a.updateSelectedCommentsMap=t=>{try{c.selectedCommentsMap=t,c.subscribers.forEach((t=>{"function"==typeof t&&t(c.selectedCommentsMap)}))}catch(t){c.catchError("Error updating selected comments map:",t)}};class s{constructor(o){this.getSelectedText=e=>{try{return e.selection?t.Editor.string(e,e.selection):null}catch(t){return a.catchError("Error getting selected text:",t),null}},this.findOccurrenceOfText=t=>{try{const{searchText:e,scopeElement:o,startOffset:r=0,caseSensitive:n=!0}=t;if(!e)return{total:0,position:0};const i="string"==typeof o?o:(null==o?void 0:o.textContent)||"",c=n?t=>t:t=>t.toLowerCase(),a=c(i),s=c(e),l=[];let d=0;for(;;){const t=a.indexOf(s,d);if(-1===t)break;l.push(t),d=t+s.length}const u=l.filter((t=>t<r)).length+1;return{total:l.length,position:u}}catch(t){return a.catchError("Error finding occurrence of text:",t),null}},this.findOccurrenceOfSelectedText=(o,r)=>{var n;try{const i=this.getSelectedText(o);if(!i)return 0;if(!r){let e=0;const r=this.findAllMatches(o,i),n=o.selection;if(n){const o=t.Path.compare(n.anchor.path,n.focus.path)>0||t.Path.equals(n.anchor.path,n.focus.path)&&n.anchor.offset>n.focus.offset,i=r.findIndex((t=>o?t.anchor.path.toString()===n.focus.path.toString()&&t.focus.path.toString()===n.anchor.path.toString()&&t.anchor.offset===n.focus.offset&&t.focus.offset===n.anchor.offset:t.anchor.path.toString()===n.anchor.path.toString()&&t.focus.path.toString()===n.focus.path.toString()&&t.anchor.offset===n.anchor.offset&&t.focus.offset===n.focus.offset));-1!==i&&(e=i+1)}return e}const c=e.ReactEditor.toDOMNode(o,o),a=null===(n=o.selection)||void 0===n?void 0:n.anchor.offset,s=this.findOccurrenceOfText({searchText:i,scopeElement:r||c,startOffset:a});return null==s?void 0:s.position}catch(t){return a.catchError("Error finding occurrence of selected text:",t),null}},this.findAllMatches=(e,o,r={caseSensitive:!0})=>{try{const{caseSensitive:n=!0}=r,i=[];if(!o)return i;const c=t=>"object"==typeof t&&null!==t&&"text"in t?t.text:"",s=t=>"object"==typeof t&&null!==t&&"text"in t&&"string"==typeof t.text,l=(t,e)=>{if(s(t)){const r=c(t),a=n?o:o.toLowerCase(),s=n?r:r.toLowerCase();let l=0;for(;;){const t=s.indexOf(a,l);if(-1===t)break;const r={anchor:{path:e,offset:t},focus:{path:e,offset:t+o.length}};i.push(r),l=t+1}}"children"in t&&Array.isArray(t.children)&&t.children.forEach(((t,o)=>{l(t,[...e,o])}))};try{const[o]=t.Editor.nodes(e,{at:[],mode:"highest"});o&&l(o[0],o[1])}catch(t){a.catchError("Error searching in document:",t)}return i}catch(t){return a.catchError("Error finding all matches:",t),[]}},this.findParentNodeWithIdForSelection=e=>{try{const{selection:o}=e;if(!o)return null;const{anchor:r}=o,n=t.Node.get(e,r.path);return this.findParentNodeWithId(e,{node:n,path:r.path})}catch(t){return a.catchError("Error finding parent node with ID for selection:",t),null}},this.findParentNodeWithId=(o,r)=>{var n;try{let{node:i,path:c}=r||{};if(!i)return null;c&&(i=t.Editor.node(o,c)[0]);let a=null,s=null;try{a=e.ReactEditor.toDOMNode(o,i)}catch(t){return null}try{s=e.ReactEditor.toDOMNode(o,o)}catch(t){return null}const l=null!==(n=null==r?void 0:r.maxIterations)&&void 0!==n?n:100,d=null==r?void 0:r.idPattern;let u=a,h=0;for(;u&&u!==document.body&&h<l;){if(!s.contains(u))return null;if(u.id&&(!d||d.test(u.id)))return u;u=u.parentElement,h++}return null}catch(t){return a.catchError("Error finding parent node with ID:",t),null}},this.removeVeltCommentFromEditor=(o,r)=>{try{if(!o||!r)throw new Error("Invalid input: editor and annotationId are required");const n=[];for(const[e,i]of t.Node.descendants(o))s.isVeltCommentText(e)&&e.annotationId===r&&n.push(i);try{n.reverse().forEach((r=>{const n=t.Editor.node(o,r)[0],i=e.ReactEditor.toDOMNode(o,n);if(null==i?void 0:i.isConnected){const e=()=>{t.Transforms.unwrapNodes(o,{at:r,mode:"lowest"})},n=s.getHistoryEditor();n&&"function"==typeof n.withoutSaving?n.withoutSaving(o,(()=>{e()})):e()}}))}catch(t){a.catchError("Error unwrapping elements:",t)}return!0}catch(t){return a.catchError("Error removing Velt comment:",t),!1}},this.highlightTextWithVeltComment=(t,e)=>{var o;try{const{text:r,targetTextNodeId:n,occurrence:i}=(null===(o=null==e?void 0:e.context)||void 0===o?void 0:o.textEditorConfig)||{};if(!r||!i)return;this.injectTag(t,r,i,e.annotationId,n)&&e.annotationId&&this.plugin.storeService.annotationStore.set(e.annotationId,{annotation:e})}catch(t){a.catchError("Error highlighting text with velt comment:",t)}},this.getEditorTextContent=e=>{try{return e.children.map((e=>t.Node.string(e))).join("")}catch(t){return a.catchError("Error getting editor text content:",t),""}},this.onSlateValueChange=({editor:e})=>{try{const o=this.collectDocumentAnnotations({editor:e});if(0===o.size)return;const r=new Map;for(const[n]of o){if(!this.plugin.storeService.annotationStore.get(n))continue;if(!Array.from(t.Editor.nodes(e,{at:[]})).filter((([t])=>s.isVeltCommentText(t))).filter((([t])=>t.annotationId===n)).map((([o,r])=>({node:o,pos:t.Editor.start(e,r).offset}))).length)continue;const o=this.processAnnotationChanges({editor:e,annotationId:n});o&&r.set(n,o)}r.size>0&&this.updateVeltComments({changes:r,editor:e})}catch(t){a.catchError("Error in processVeltCommentChanges:",t)}},this.injectTag=(o,r,n,c,l)=>{try{let a=null;if(l){if(a=document.getElementById(l),!a)return!1;if(!this.isElementInEditor(o,a))return!1}else a=e.ReactEditor.toDOMNode(o,o);if(!a)return!1;const d=this.findTextOccurrence(o,r,n,a);if(!d)return!1;if(Array.from(t.Editor.nodes(o,{at:d,match:t=>s.isVeltCommentText(t)})).length>0)return!1;const u=()=>{o.wrapNodes({type:i.VELT_COMMENT,children:[],annotationId:c},{at:d,split:!0})},h=s.getHistoryEditor();return h&&"function"==typeof h.withoutSaving?h.withoutSaving(o,(()=>{u()})):u(),!0}catch(t){return a.catchError("Error injecting tag:",t),!1}},this.plugin=o}collectDocumentAnnotations({editor:e}){try{const o=new Map;return Array.from(t.Editor.nodes(e,{at:[]})).filter((([t])=>s.isVeltCommentText(t))).forEach((([t])=>{const e=t.annotationId;if(e){const t=this.plugin.storeService.annotationStore.get(e);(null==t?void 0:t.annotation)&&o.set(e,t.annotation)}})),o}catch(t){return a.catchError("Error collecting document annotations:",t),new Map}}processAnnotationChanges({editor:o,annotationId:r}){var n,i,c,l;try{let a=null,d=null,u=null;const h=null===(n=this.plugin.storeService.annotationStore.get(r))||void 0===n?void 0:n.annotation;if(!(null===(i=null==h?void 0:h.context)||void 0===i?void 0:i.textEditorConfig))return null;const{text:f,targetTextNodeId:g,occurrence:m}=(null===(c=null==h?void 0:h.context)||void 0===c?void 0:c.textEditorConfig)||{},E=Array.from(t.Editor.nodes(o,{at:[],match:t=>s.isVeltCommentText(t)&&t.annotationId===r,mode:"lowest"}));for(const[t,e]of E)a=t,u=e;if(!a)return null;try{if(d=e.ReactEditor.toDOMNode(o,a),!d)return null}catch(t){return null}const p=E.map((([e])=>t.Node.string(e))).join("");if(!p)return null;const v=this.findParentNodeWithId(o,{node:a,path:null!=u?u:void 0});let x,C,S;v&&(x=v.id),v&&(C=e.ReactEditor.toSlateNode(o,v),S=e.ReactEditor.findPath(o,C));const T=null===(l=this.findTextOccurrenceOfNode(o,a,null!=u?u:void 0,C,S))||void 0===l?void 0:l.nodeOccurrencePosition;if(!T)return null;const b=p!==f,y=T!==m,I=g!==x;return b||y||I?{contentChanged:b,currentText:p,occurrenceChanged:y,currentOccurrence:null!=T?T:0,originalOccurrence:m,annotationId:r,originalText:f,originalTargetTextNodeId:g,newTargetTextNodeId:x,annotation:h,multiThreadAnnotationId:null==h?void 0:h.multiThreadAnnotationId,targetTextNodeIdChanged:I}:null}catch(t){return a.catchError("Error processing annotation changes:",t),null}}updateVeltComments({changes:t,editor:e}){var o;try{for(const[r,n]of Array.from(t.entries()))try{if(!(null===(o=n.annotation.context)||void 0===o?void 0:o.textEditorConfig))continue;const t=JSON.parse(JSON.stringify(n.annotation.context.textEditorConfig));n.contentChanged&&(t.text=n.currentText),n.occurrenceChanged&&(t.occurrence=n.currentOccurrence),n.targetTextNodeIdChanged&&(t.targetTextNodeId=n.newTargetTextNodeId);const i=Object.assign(Object.assign({},n.annotation.context),{textEditorConfig:t});this.plugin.storeService.annotationStore.remove(r),this.removeVeltCommentFromEditor(e,r),a.updateContext({annotationId:r,context:i})}catch(t){a.catchError("Error updating Velt comments",t)}}catch(t){a.catchError("Error updating Velt comments",t)}}isElementInEditor(t,o){try{return e.ReactEditor.toDOMNode(t,t).contains(o)}catch(t){return a.catchError("Error checking element in editor:",t),!1}}traverseNodesForText(e,o,r,n){try{if(t.Text.isText(e)){const t=n.length,i=(n+=e.text).length;r.push({node:e,path:o,start:t,end:i})}else if(t.Element.isElement(e))for(let t=0;t<e.children.length;t++){const i=[...o,t];n=this.traverseNodesForText(e.children[t],i,r,n)}return n}catch(t){return a.catchError("Error traversing nodes for text:",t),""}}collectSubtreeText(t,e,o){try{const r=[];let n="";if(0===o.length)for(let e=0;e<t.children.length;e++){const o=[e];n=this.traverseNodesForText(t.children[e],o,r,n)}else n=this.traverseNodesForText(e,o,r,n);return{fullText:n,textNodePositions:r}}catch(t){return a.catchError("Error collecting subtree text:",t),{fullText:"",textNodePositions:[]}}}findTextOccurrence(t,o,r,n){try{const i=e.ReactEditor.toSlateNode(t,n),c=e.ReactEditor.findPath(t,i),{fullText:a,textNodePositions:s}=this.collectSubtreeText(t,i,c),l=[];let d;const u=new RegExp(o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g");for(;null!==(d=u.exec(a));)l.push({index:d.index});if(l.length<r)return null;const h=l[r-1].index,f=h+o.length;let g,m;for(const t of s)if(h>=t.start&&h<t.end&&(g=t),f>t.start&&f<=t.end&&(m=t),g&&m)return{anchor:{path:g.path,offset:h-g.start},focus:{path:m.path,offset:f-m.start}};return null}catch(t){return a.catchError("Error in findTextOccurrence:",t),null}}findAllTextOccurrences(e,o,r=!0,n,i){try{if(!o)return[];const c=[],s=r?t=>t:t=>t.toLowerCase(),l=s(o);if(n&&!i)try{for(const[o,r]of t.Editor.nodes(e))if(o===n){i=r;break}}catch(t){a.catchError("Error finding container path:",t)}const d=i||[];for(const[r,n]of t.Editor.nodes(e,{at:d,match:e=>t.Text.isText(e)})){if(i&&!t.Path.isDescendant(n,i)&&!t.Path.equals(n,i))continue;const e=t.Node.string(r),a=s(e);let d,u=0;for(;-1!==(d=a.indexOf(l,u));){const t={path:n,offset:d},i={path:n,offset:d+o.length},a={anchor:t,focus:i};c.push({node:r,path:n,range:a,startPoint:t,endPoint:i,text:e.substring(d,d+o.length)}),u=d+l.length}}return c}catch(t){return a.catchError("Error finding all text occurrences:",t),[]}}findTextOccurrenceOfNode(o,r,n,i,c){try{const s=t.Node.string(r);if(!s.trim())return{node:r,nodePath:n,text:s,totalOccurrences:0,nodeOccurrencePosition:0,textOccurrences:[],containerNode:i,containerPath:c};if(!n)try{if("findPath"in e.ReactEditor)try{n=e.ReactEditor.findPath(o,r)}catch(t){a.catchError("Error finding path with ReactEditor:",t)}if(!n)for(const[e,i]of t.Editor.nodes(o))if(e===r){n=i;break}}catch(t){a.catchError("Error finding node path:",t)}const l=this.findAllTextOccurrences(o,s,!0,i,c);let d=0;if(n)for(let e=0;e<l.length;e++){const o=l[e],r=t.Path.equals(o.path,n),i=t.Path.isAncestor(n,o.path);if(r||i){d=e+1;break}}return{node:r,nodePath:n,text:s,totalOccurrences:l.length,nodeOccurrencePosition:d,textOccurrences:l,containerNode:i,containerPath:c}}catch(t){return a.catchError("Error finding text occurrence of node:",t),null}}}s.setHistoryEditor=t=>{s.HistoryEditor=t},s.getHistoryEditor=()=>s.HistoryEditor,s.getEditorId=t=>{var o,r;try{const i=e.ReactEditor.toDOMNode(t,t);return i&&(null!==(r=null===(o=null==i?void 0:i.closest(`[${n.EDITOR_ID}]`))||void 0===o?void 0:o.getAttribute(n.EDITOR_ID))&&void 0!==r)?r:null}catch(t){return a.catchError("Error finding editor ID:",t),null}},s.isVeltCommentText=t=>t.type===i.VELT_COMMENT;class l{constructor(t){this.isSubscribed=!1,this.globalStore={editor:null,comments:[],filteredComments:[],selectedCommentsMap:new Map},this.globalAnnotationStore=new Map,this.annotationStore={get:t=>this.globalAnnotationStore.get(t),set:(t,e)=>{this.globalAnnotationStore.set(t,e)},getAll:()=>new Map(this.globalAnnotationStore),clear:()=>{this.globalAnnotationStore.clear()},remove:t=>{this.globalAnnotationStore.delete(t)}},this.updateGlobalStore=t=>{var e,o,r;try{const n=(null==t?void 0:t.comments)||(null===(e=this.globalStore)||void 0===e?void 0:e.comments)||[],i=(null==t?void 0:t.selectedCommentsMap)||(null===(o=this.globalStore)||void 0===o?void 0:o.selectedCommentsMap)||new Map,c=(null===(r=this.globalStore)||void 0===r?void 0:r.filteredComments)||[],a=n.filter((t=>{var e;return"terminal"!==(null===(e=null==t?void 0:t.status)||void 0===e?void 0:e.type)||(null==i?void 0:i.get(null==t?void 0:t.annotationId))})),s=new Set(a.map((t=>null==t?void 0:t.annotationId)));c.map((t=>null==t?void 0:t.annotationId)).filter((t=>t&&!s.has(t))).forEach((t=>{var e,o;(null===(e=this.globalStore)||void 0===e?void 0:e.editor)&&t&&this.plugin.editorService.removeVeltCommentFromEditor(null===(o=this.globalStore)||void 0===o?void 0:o.editor,t)})),this.globalStore=Object.assign(Object.assign(Object.assign({},this.globalStore),t),{filteredComments:a}),this.highlightCommentsFromGlobalStore()}catch(t){a.catchError("Error updating global store:",t)}},this.highlightCommentsFromGlobalStore=()=>{var t,e;try{const{editor:o,filteredComments:r}=this.globalStore;if(!o||!r)return;for(const n of r){(null===(e=null===(t=null==n?void 0:n.context)||void 0===t?void 0:t.textEditorConfig)||void 0===e?void 0:e.text)&&this.plugin.editorService.highlightTextWithVeltComment(o,n)}}catch(t){a.catchError("Error highlighting comments from global store:",t)}},this.plugin=t;try{a.subscribeToSelectedCommentsMap(this.plugin.editorId,(t=>{try{this.updateGlobalStore({selectedCommentsMap:t})}catch(t){a.catchError("Error updating global store:",t)}}))}catch(t){a.catchError("Error subscribing to selected comments map:",t)}}}l.storeInstanceMap=new Map;const d=new Map;class u{constructor(t,e){this.previousEditorTextContent="",this.editorId=t,this.editor=e,this.editorService=new s(this),this.storeService=new l(this),e&&this.setPreviousEditorTextContent(this.editorService.getEditorTextContent(e))}static getInstance(t){let{editorId:e,editor:o}=t||{};return e||(e="default"),d.has(e)||d.set(e,new u(e,o)),d.get(e)}getPreviousEditorTextContent(){return this.previousEditorTextContent}setPreviousEditorTextContent(t){this.previousEditorTextContent=t}}const h=t=>r(void 0,[t],void 0,(function*({editorId:t,editor:e,context:o}){var r,n;try{if(!a.getCommentElement())return;t||(t=null!==(r=s.getEditorId(e))&&void 0!==r?r:void 0);const i=u.getInstance({editorId:t,editor:e}),c=i.editorService.getSelectedText(e);if(!c)return;const l=i.editorService.findParentNodeWithIdForSelection(e);let d,h=null;l&&(d=l.id,h=null===(n=l.closest("[data-velt-location-id]"))||void 0===n?void 0:n.getAttribute("data-velt-location-id"));const f=i.editorService.findOccurrenceOfSelectedText(e,null!=l?l:void 0);if(!f)return;let g,m={};o&&"object"==typeof o&&(m=Object.assign(Object.assign({},m),o)),m.textEditorConfig={text:c,occurrence:f,targetTextNodeId:d},t&&(m.textEditorConfig.editorId=t),yield new Promise((t=>setTimeout(t,200))),h&&(g={id:h});try{a.addManualComment({context:m,location:g})}catch(t){a.catchError("Error creating comment",t)}}catch(t){a.catchError("Error adding comment",t)}}));const f=({editor:t,editorId:e,commentAnnotations:o})=>{var r;try{if(t){e||(e=null!==(r=s.getEditorId(t))&&void 0!==r?r:void 0),o=null==o?void 0:o.filter((t=>{var o,r,n;return!!(null===(o=t.context)||void 0===o?void 0:o.textEditorConfig)&&(null===(n=null===(r=t.context)||void 0===r?void 0:r.textEditorConfig)||void 0===n?void 0:n.editorId)===e}));u.getInstance({editorId:e,editor:t}).storeService.updateGlobalStore({editor:t,comments:o}),a.subscribeToSelectedAnnotationsMapSingleton()}}catch(t){a.catchError("Error highlighting tiptap-velt-comments",t)}},g=({editor:t,editorId:e})=>{var o;try{e||(e=null!==(o=s.getEditorId(t))&&void 0!==o?o:void 0);const r=u.getInstance({editorId:e,editor:t}),n=r.editorService.getEditorTextContent(t);if(n===r.getPreviousEditorTextContent())return;r.setPreviousEditorTextContent(n),r.editorService.onSlateValueChange({editor:t})}catch(t){a.catchError("Error in processVeltCommentChanges:",t)}};exports.SlateVeltComment=t=>{const{attributes:e,children:r,element:n}=t,i=null==n?void 0:n.annotationId;return o.createElement("velt-comment-text",Object.assign({"annotation-id":i},e),r)},exports.addComment=h,exports.highlightComments=function(t,e,o){try{f({editor:t,editorId:o,commentAnnotations:e})}catch(t){a.catchError("Error highlighting tiptap-velt-comments",t)}},exports.onSlateValueChange=g,exports.renderComments=f,exports.triggerAddComment=(t,e)=>r(void 0,void 0,void 0,(function*(){try{return h({editor:t,context:null==e?void 0:e.context})}catch(t){a.catchError("Error triggering add comment",t)}})),exports.withVeltComments=(e,o)=>{const{isInline:r,isVoid:n,onChange:i}=e;return(null==o?void 0:o.HistoryEditor)&&s.setHistoryEditor(o.HistoryEditor),e.isInline=t=>!!s.isVeltCommentText(t)||r(t),e.isVoid=t=>!s.isVeltCommentText(t)&&n(t),e.onChange=()=>{i&&i();e.operations.some((e=>t.Operation.isTextOperation(e)))&&g({editor:e})},e};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|