pipeup 0.5.1 → 0.5.3
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 +2 -2
- package/dist/pipeup.core.js +1 -10
- package/dist/pipeup.esm.js +1 -10
- package/dist/pipeup.min.js +1 -10
- package/dist/types/core.d.ts +4 -2
- package/dist/types/model/animals.d.ts +3 -0
- package/dist/types/ui/addons.d.ts +15 -0
- package/dist/types/ui/animals.d.ts +1 -1
- package/dist/types/ui/dom.d.ts +7 -0
- package/dist/types/ui/notes.d.ts +12 -0
- package/dist/types/ui/styles.d.ts +1 -1
- package/dist/types/ui/thread-view.d.ts +2 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Comments and feedback right on any HTML page: documents, slide decks and whole sites.
|
|
4
4
|
|
|
5
|
-
> **Alpha (0.5.
|
|
5
|
+
> **Alpha (0.5.3).** The API and the stored comment format may change before 1.0; pin an exact version.
|
|
6
6
|
> Website and Try pages: https://pipeup-ai.github.io/pipeup/ · Source: https://github.com/pipeup-ai/pipeup
|
|
7
7
|
|
|
8
8
|
Pipeup: signed comment operations, threads with one-level replies, anchors that survive page changes,
|
|
@@ -21,7 +21,7 @@ As a classic script (also works from `file://`), it mounts itself on pages that
|
|
|
21
21
|
```html
|
|
22
22
|
<html data-pipeup-doc="<id>:<key>">
|
|
23
23
|
...
|
|
24
|
-
<script src="https://cdn.jsdelivr.net/npm/pipeup@0.5.
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/pipeup@0.5.3/dist/pipeup.min.js" integrity="sha384-…" crossorigin="anonymous" defer></script>
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
Each release's notes on GitHub give the `integrity` value
|
package/dist/pipeup.core.js
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
function f(t){let e="";for(let r of t)e+=String.fromCharCode(r);return btoa(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function g(t){let e=atob(t.replace(/-/g,"+").replace(/_/g,"/")),r=new Uint8Array(e.length);for(let n=0;n<e.length;n++)r[n]=e.charCodeAt(n);return r}function J(t=16){return f(crypto.getRandomValues(new Uint8Array(t)))}function m(t){return new TextEncoder().encode(t)}function x(t){if(t===null||typeof t!="object")return JSON.stringify(t);if(Array.isArray(t))return"["+t.map(x).join(",")+"]";let e=t;return"{"+Object.keys(e).filter(r=>e[r]!==void 0).sort().map(r=>JSON.stringify(r)+":"+x(e[r])).join(",")+"}"}function y(t){return t.replace(/\s+/g," ").trim()}function A(t){let e=y(t),r=2166136261;for(let n=0;n<e.length;n++)r^=e.charCodeAt(n),r=Math.imul(r,16777619)>>>0;return r.toString(16).padStart(8,"0")}var Te=/^[A-Za-z0-9_-]{43}$/,Ie=512,w=new Map;async function R(){let t=await crypto.subtle.generateKey({name:"Ed25519"},!1,["sign","verify"]),e=new Uint8Array(await crypto.subtle.exportKey("raw",t.publicKey));return{publicKey:f(e),privateKey:t.privateKey}}async function S(t,e){let r=await crypto.subtle.sign({name:"Ed25519"},t.privateKey,e);return f(new Uint8Array(r))}async function Q(t,e,r){if(!Te.test(t))return!1;try{if(f(g(t))!==t)return!1;let n=w.get(t);if(!n){w.size>=Ie&&w.delete(w.keys().next().value),n=crypto.subtle.importKey("raw",g(t),{name:"Ed25519"},!0,["verify"]),w.set(t,n);let o=n;o.catch(()=>{w.get(t)===o&&w.delete(t)})}return await crypto.subtle.verify({name:"Ed25519"},await n,g(r),e)}catch{return!1}}var k=1e4,Ce=2**18,D=2**40,L=2**47,p={doc:64,path:2e3,snapshot:500,anchorId:200,quote:1e4,context:64,viewEntries:16,viewKey:64,viewValue:200},Me=/^[0-9a-f]{8}$/,F=/^[A-Za-z0-9_-]{43}$/,$e=/^[A-Za-z0-9_-]{43}$/,Ne=new Set(["create","reply","edit","delete","resolve","reopen"]);async function Y(t,e){if(t.author!==e.publicKey)throw new Error("pipeup: an op must be signed by its author");return{body:t,sig:await S(e,m(x(t)))}}var h=(t,e)=>typeof t=="string"&&t.length<=e,O=t=>!!t&&typeof t=="object",V=t=>typeof t=="number"&&Number.isFinite(t)&&t>=0&&t<=1;function B(t){if(!O(t))return!1;let{id:e,path:r,fingerprint:n,snapshot:o,quote:i,point:s,view:a}=t;if(!h(r,p.path)||!h(o,p.snapshot)||typeof n!="string"||!Me.test(n)||e!==void 0&&(!h(e,p.anchorId)||e==="")||i!==void 0&&(!O(i)||!h(i.exact,p.quote)||i.exact===""||!h(i.prefix,p.context)||!h(i.suffix,p.context))||s!==void 0&&(!O(s)||!V(s.x)||!V(s.y)))return!1;if(a!==void 0){if(!O(a)||Array.isArray(a))return!1;let d=Object.entries(a);if(d.length>p.viewEntries||!d.every(([c,l])=>c.length<=p.viewKey&&h(l,p.viewValue)))return!1}return!0}function W(t){if(!O(t))return!1;let{body:e,sig:r}=t;if(typeof r!="string"||!O(e)||JSON.stringify({body:e,sig:r}).length>Ce)return!1;let n=e;return!(n.v!==1||typeof n.id!="string"||!F.test(n.id)||!Ne.has(n.kind)||!h(n.doc,p.doc)||n.doc===""||typeof n.thread!="string"||!F.test(n.thread)||typeof n.author!="string"||!$e.test(n.author)||typeof n.name!="string"||n.name.length>80||n.name!==n.name.trim()||typeof n.clock!="number"||!Number.isSafeInteger(n.clock)||n.clock<1||n.clock>D||typeof n.at!="number"||!Number.isSafeInteger(n.at)||n.at<0||n.at>L||n.text!==void 0&&(!h(n.text,k)||n.text!==n.text.trim())||n.target!==void 0&&(typeof n.target!="string"||!F.test(n.target))||(n.kind==="create"||n.kind==="reply"||n.kind==="edit")&&!n.text||(n.kind==="reply"||n.kind==="edit"||n.kind==="delete")&&!n.target||n.kind==="create"&&(n.id!==n.thread||!B(n.anchor)))}function ee(t){return Q(t.body.author,m(x(t.body)),t.sig)}function te(t,e){return t.body.clock!==e.body.clock?t.body.clock-e.body.clock:t.body.at!==e.body.at?t.body.at-e.body.at:t.body.author!==e.body.author?t.body.author<e.body.author?-1:1:t.body.id!==e.body.id?t.body.id<e.body.id?-1:1:0}function re(t){let e=new Map,r=new Map,n=new Map;for(let{body:o}of[...t].sort(te)){if(o.name&&n.set(o.author,o.name),o.kind==="create"){if(e.has(o.id)||!o.anchor||o.text===void 0)continue;let a=ne(o.id,o.author,o.name,o.text,o.at);e.set(o.id,{id:o.id,anchor:o.anchor,root:a,resolved:!1}),r.set(o.id,{comment:a,thread:o.id});continue}let i=e.get(o.thread);if(!i)continue;if(o.kind==="resolve"||o.kind==="reopen"){i.resolved=o.kind==="resolve";continue}let s=o.target?r.get(o.target):void 0;if(!(!s||s.thread!==o.thread)){if(o.kind==="reply"){if(r.has(o.id)||o.text===void 0)continue;let a=ne(o.id,o.author,o.name,o.text,o.at);i.root.replies.push(a),r.set(o.id,{comment:a,thread:o.thread});continue}o.author!==s.comment.author||s.comment.deleted||(o.kind==="edit"&&o.text!==void 0&&(s.comment.text=o.text,s.comment.edited=!0),o.kind==="delete"&&(s.comment.text="",s.comment.deleted=!0))}}for(let{comment:o}of r.values())o.name||(o.unnamed=!0,o.name=n.get(o.author)??"");return[...e.values()]}function ne(t,e,r,n,o){return{id:t,author:e,name:r,text:n,at:o,edited:!1,deleted:!1,replies:[]}}async function _(t){let e={...t,id:void 0,thread:t.kind==="create"?void 0:t.thread},r=await crypto.subtle.digest("SHA-256",m(x(e)));return f(new Uint8Array(r))}var T=class{constructor(e){this.doc=e}ops=new Map;clock=0;createAt=new Map;threadAt=new Map;get size(){return this.ops.size}all(){return[...this.ops.values()]}threads(){return re(this.ops.values())}async add(e){let r=[];for(let n of e){if(!W(n)||n.body.doc!==this.doc||this.ops.has(n.body.id)||n.body.id!==await _(n.body)||!await ee(n))continue;let o={body:n.body,sig:n.sig};this.ops.set(o.body.id,o),this.clock=Math.max(this.clock,o.body.clock),o.body.kind==="create"&&this.createAt.set(o.body.author,Math.max(this.createAt.get(o.body.author)??0,o.body.at)),this.threadAt.set(o.body.thread,Math.max(this.threadAt.get(o.body.thread)??0,o.body.at)),r.push(o)}return r}async append(e,r,n){let o={v:1,...n,id:"",thread:n.kind==="create"?"":n.thread,doc:this.doc,author:e.publicKey,name:r,clock:Math.min(this.clock+1,D),at:Math.min(Math.max(Date.now(),n.kind==="create"?(this.createAt.get(e.publicKey)??0)+1:(this.threadAt.get(n.thread)??0)+1),L)};o.id=await _(o);let i=this.ops.get(o.id);if(i)return i;o.kind==="create"&&(o.thread=o.id);let[s]=await this.add([await Y(o,e)]);if(!s)throw new Error("pipeup: could not record this change");return s}};var Re=/^([A-Za-z0-9_-]{16}):([A-Za-z0-9_-]{43})$/;function Fe(){return`${J(12)}:${f(crypto.getRandomValues(new Uint8Array(32)))}`}async function De(t){let e=Re.exec(t.trim()),r=e?.[1],n=e?.[2];if(!r||!n)throw new Error('pipeup: data-pipeup-doc must look like "<id>:<key>", as made by newDocumentAttribute()');let o=await crypto.subtle.importKey("raw",g(n),{name:"AES-GCM"},!1,["encrypt","decrypt"]);return{id:r,key:o}}async function oe(t,e,r){let n=crypto.getRandomValues(new Uint8Array(12)),o=await crypto.subtle.encrypt({name:"AES-GCM",iv:n,additionalData:m(r)},t,m(e));return{iv:f(n),data:f(new Uint8Array(o))}}async function ie(t,e,r){try{let n=await crypto.subtle.decrypt({name:"AES-GCM",iv:g(e.iv),additionalData:m(r)},t,g(e.data));return new TextDecoder().decode(n)}catch{throw new Error("pipeup: this feedback could not be opened with this document")}}var Le=5e6,se=t=>`pipeup-file:${t}`;async function ae(t,e,r){let n=r?{pipeup:1,doc:t,sealed:!0,...await oe(r,JSON.stringify(e),se(t))}:{pipeup:1,doc:t,sealed:!1,ops:e};return JSON.stringify(n,null,2)}async function de(t,e,r){if(t.length>Le)throw new Error("pipeup: this feedback file is too large");let n;try{n=JSON.parse(t)}catch{throw new Error("pipeup: this is not an Pipeup feedback file")}if(!n||typeof n!="object"||n.pipeup!==1||typeof n.doc!="string")throw new Error("pipeup: this is not an Pipeup feedback file");if(n.doc!==e)throw new Error("pipeup: this feedback is for a different document");if(!n.sealed)return Array.isArray(n.ops)?n.ops:[];if(!r)throw new Error("pipeup: this feedback is sealed, and this page has no document key");if(typeof n.iv!="string"||typeof n.data!="string")throw new Error("pipeup: this is not an Pipeup feedback file");let o;try{o=await ie(r,{iv:n.iv,data:n.data},se(e))}catch{throw new Error("pipeup: this feedback could not be opened with this document's key")}let i;try{i=JSON.parse(o)}catch{throw new Error("pipeup: this is not an Pipeup feedback file")}return Array.isArray(i)?i:[]}var Be=5e3,_e=/^[a-z][a-z0-9-]{1,23}\/[a-z][a-z0-9-]{0,31}$/,ce=80,E=class extends Error{constructor(r,n,o=1){super("pipeup: shown here, but not saved yet");this.id=r;this.cause=n;this.count=o;this.name="UnsavedChangeError"}},K=class t{constructor(e,r){this.log=e;this.options=r;this.displayName=r.name.trim()?pe(r.name):""}listeners=new Set;cache=[];comments=new Map;displayName;e=Promise.resolve();unsaved=[];static async open(e){if(typeof e.doc!="string"||e.doc===""||e.doc.length>p.doc)throw new Error(`pipeup: the document id must be 1 to ${p.doc} characters`);let r=new T(e.doc),n=await e.store.load(e.doc),o=n.length-(await r.add(n)).length;o&&console.warn(`pipeup: ${o} saved change${o===1?"":"s"} on this page couldn't be read; kept as saved`);let i=new t(r,e);return i.refresh(!1),i}get id(){return this.options.doc}get me(){return this.options.identity.publicKey}get name(){return this.displayName}set name(e){this.displayName=pe(e),this.refresh(!0,[],"")}threads(){return this.cache}ops(){return this.log.all()}onChange(e){return this.listeners.add(e),()=>this.listeners.delete(e)}comment(e,r){return this.enqueue(()=>this.write({kind:"create",anchor:e,text:U(r)}))}reply(e,r){return this.enqueue(()=>this.write({kind:"reply",thread:this.find(e).thread,target:e,text:U(r)}))}edit(e,r){return this.enqueue(async()=>{let{thread:n}=this.mine(e);await this.write({kind:"edit",thread:n,target:e,text:U(r)})})}remove(e){return this.enqueue(async()=>{let{thread:r}=this.mine(e);await this.write({kind:"delete",thread:r,target:e})})}resolve(e){return this.enqueue(async()=>{await this.write({kind:"resolve",thread:this.thread(e)})})}reopen(e){return this.enqueue(async()=>{await this.write({kind:"reopen",thread:this.thread(e)})})}sign(e,r){if(!_e.test(e))throw new Error('pipeup: a signing purpose looks like "<add-on id>/<name>"');return S(this.options.identity,m(`pipeup:${e}
|
|
2
|
-
${r}`))}flush(){return this.enqueue(()=>this.persist())}exportFile(){return ae(this.options.doc,this.log.all(),this.options.key)}async importFile(e){return this.merge(await de(e,this.options.doc,this.options.key),"file")}merge(e,r){return e.length>Be?Promise.reject(new RangeError("pipeup: too many changes to merge at once")):this.enqueue(async()=>{let n=await this.log.add([...e]);this.unsaved.push(...n),n.length>0&&this.refresh(!0,n,r);try{await this.persist()}catch(o){throw n.length>0?new E(n[0].body.id,o,n.length):o}return n.length})}enqueue(e){let r=this.e.then(e);return this.e=r.catch(()=>{}),r}async write(e){let r=await this.log.append(this.options.identity,this.displayName,e);this.unsaved.push(r),this.refresh(!0,[r],"local");try{await this.persist()}catch(n){throw new E(r.body.id,n)}return r.body.id}async persist(){if(this.unsaved.length===0)return;let e=[...this.unsaved];await this.options.store.append(this.options.doc,e),this.unsaved=this.unsaved.filter(r=>!e.includes(r))}refresh(e,r=[],n=""){this.cache=this.log.threads(),this.comments.clear();let o=(i,s)=>{i.unnamed&&i.author===this.me&&this.displayName&&(i.name=this.displayName),this.comments.set(i.id,{comment:i,thread:s});for(let a of i.replies)o(a,s)};for(let i of this.cache)o(i.root,i.id);if(e)for(let i of this.listeners)try{i(this.cache,r,n)}catch(s){globalThis.reportError?.(s)}}find(e){let r=this.comments.get(e);if(!r)throw new Error("pipeup: there is no comment with that id");return r}mine(e){let r=this.find(e);if(r.comment.author!==this.me)throw new Error("pipeup: only the writer can change a comment");return r}thread(e){if(!this.cache.some(r=>r.id===e))throw new Error("pipeup: there is no thread with that id");return e}};function U(t){let e=t.trim();if(!e)throw new Error("pipeup: a comment needs some words");if(e.length>k)throw new Error(`pipeup: comments are limited to ${k} characters`);return e}function pe(t){let e=t.replace(/\s+/g," ").trim();if(!e||e.length>ce)throw new Error(`pipeup: choose a name of 1 to ${ce} characters`);return e}var q=class{docs=new Map;profile=null;async load(e){return[...this.docs.get(e)?.values()??[]]}async append(e,r){let n=this.docs.get(e);n||this.docs.set(e,n=new Map);for(let o of r)n.set(o.body.id,o)}async loadProfile(){return this.profile}async saveProfile(e){this.profile=e}async saveProfileIfAbsent(e){return this.profile||(this.profile=e),this.profile}},le="",Ue="Reviewer";async function Ke(t,e=le){let r=await t.loadProfile();if(r)return r.name===Ue?{...r,name:""}:r;let n={identity:await R(),name:e};return t.saveProfileIfAbsent(n)}var j=class t{constructor(e){this.db=e}static open(e="pipeup"){return new Promise((r,n)=>{let o=indexedDB.open(e,1);o.onupgradeneeded=()=>{let i=o.result;i.createObjectStore("ops",{keyPath:["doc","id"]}).createIndex("doc","doc"),i.createObjectStore("meta")},o.onsuccess=()=>{let i=o.result;i.onversionchange=()=>i.close(),r(new t(i))},o.onerror=()=>n(o.error??new Error("pipeup: could not open local storage"))})}close(){this.db.close()}async load(e){return(await this.request("ops","readonly",n=>n.index("doc").getAll(e))).map(n=>n.op)}async append(e,r){if(r.length===0)return;let n=this.db.transaction("ops","readwrite"),o=n.objectStore("ops"),i=ue(n);try{for(let s of r)o.put({doc:e,id:s.body.id,op:s})}catch(s){throw n.abort(),i.catch(()=>{}),s}await i}async loadProfile(){return await this.request("meta","readonly",e=>e.get("profile"))??null}async saveProfile(e){await this.request("meta","readwrite",r=>r.put(e,"profile"))}async saveProfileIfAbsent(e){let r=this.db.transaction("meta","readwrite"),n=r.objectStore("meta"),o=ue(r),i=e,s=n.get("profile");return s.onsuccess=()=>{s.result?i=s.result:n.put(e,"profile")},await o,i}request(e,r,n){return new Promise((o,i)=>{let s=n(this.db.transaction(e,r).objectStore(e));s.onsuccess=()=>o(s.result),s.onerror=()=>i(s.error??new Error("pipeup: local storage request failed"))})}};function ue(t){return new Promise((e,r)=>{t.oncomplete=()=>e(),t.onerror=()=>r(t.error??new Error("pipeup: local storage write failed")),t.onabort=()=>r(t.error??new Error("pipeup: local storage write aborted"))})}function v(t){let r=(t.ownerDocument??t).createTreeWalker(t,NodeFilter.SHOW_TEXT),n=[],o="";for(let i=r.nextNode();i;i=r.nextNode())n.push({node:i,start:o.length}),o+=i.data;return{text:o,nodes:n}}function z(t,e,r){let o=(t.ownerDocument??t).createRange();return o.setStart(t,0),o.setEnd(e,r),o.toString().length}function H(t,e,r){let n=fe(t,e,!1),o=fe(t,r,!0);if(!n||!o||r<e)return null;let i=n.node.ownerDocument.createRange();return i.setStart(n.node,n.offset),i.setEnd(o.node,o.offset),i}function fe(t,e,r){let n=t.nodes.find(o=>e>=o.start&&e<o.start+o.node.data.length+Number(r));return n&&{node:n.node,offset:e-n.start}}var me=32,ge=200,qe="p,li,h1,h2,h3,h4,h5,h6,td,th,blockquote,pre,figcaption,dt,dd,summary,caption";function ye(t){return t.nodeType===Node.ELEMENT_NODE?t:t.parentElement}function xe(t,e){for(let r=ye(t)?.closest("[data-pipeup-id]")??null;r;){if(!e.contains(r))return null;let n=r.getAttribute("data-pipeup-id")??"";if(n!==""&&n.length<=p.anchorId)return r;r=r.parentElement?.closest("[data-pipeup-id]")??null}return null}function I(t,e,r){return t.slice(e,r).replace(/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/g,"")}function je(t,e){let r=ze(t,e);if(r.length>p.path)throw new Error("pipeup: this element is nested too deeply to comment on \u2014 give a nearby container a data-pipeup-id");return r}function we(t){if(!B(t))throw new Error("pipeup: could not describe this place on the page");return t}function C(t,e){let r=ye(t),n=r?.closest(qe)??null;return n&&e.contains(n)?n:r&&e.contains(r)?r:e}function ze(t,e){if(t===e)return"";let r=[];for(let n=e;n&&n!==t;n=n.parentElement){let o=1;for(let i=n.previousElementSibling;i;i=i.previousElementSibling)i.tagName===n.tagName&&o++;r.unshift(`${n.tagName.toLowerCase()}:nth-of-type(${o})`)}return":scope > "+r.join(" > ")}function He(t,e,r){if(!e.contains(t.commonAncestorContainer))throw new Error("pipeup: the selection is outside the commentable area");let n=xe(t.commonAncestorContainer,e),o=n??e,{text:i}=v(o),s=z(o,t.startContainer,t.startOffset),a=z(o,t.endContainer,t.endOffset),d=i.slice(s,a);if(d==="")throw new Error("pipeup: select some text to comment on");if(d.length>p.quote)throw new Error(`pipeup: selections are limited to ${p.quote} characters`);let c=I(i,Math.max(0,s-me),s),l=I(i,a,a+me);return we({...n?{id:n.getAttribute("data-pipeup-id")??""}:{},path:"",quote:{exact:d,prefix:c,suffix:l},...r?{view:r}:{},fingerprint:A(C(t.startContainer,e).textContent??""),snapshot:I(y(c+d+l),0,ge)})}function Xe(t,e,r,n){let o=xe(t,e);return we({...o?{id:o.getAttribute("data-pipeup-id")??""}:{},path:je(o??e,t),...r?{point:{x:he(r.x),y:he(r.y)}}:{},...n?{view:n}:{},fingerprint:A(t.textContent??""),snapshot:I(y(t.textContent??""),0,ge)})}function he(t){return Number.isFinite(t)?Math.min(1,Math.max(0,t)):0}function be(t,e,r){let n=Array.from({length:e.length+1},(s,a)=>a),o=[...n],i=[e.length];for(let s=1;s<=t.length;s++){o[0]=r?0:s;for(let a=1;a<=e.length;a++){let d=t.charCodeAt(s-1)===e.charCodeAt(a-1)?0:1;o[a]=Math.min(n[a]+1,o[a-1]+1,n[a-1]+d)}i.push(o[e.length]),[n,o]=[o,n]}return i}var Ge=(t,e)=>be(t,e,!1).pop();function Ae(t,e,r){let n=e.length;if(n===0)return null;let o=-1,i=1/0;if(be(t,e,!0).forEach((d,c)=>{c&&d<i&&(i=d,o=c)}),i>r)return null;let s=Math.max(0,o-n),a=1/0;for(let d=Math.max(0,o-n-r);d<=Math.min(o,o-n+r);d++){let c=Ge(t.slice(d,o),e);c<a&&(a=c,s=d)}return{start:s,end:o,distance:a}}var P={state:"orphaned",element:null,range:null},Ze=5e4,Je=300,Qe=12,Ve=":scope > ";function Ye(t,e){for(let r of t.querySelectorAll("[data-pipeup-id]"))if(r.getAttribute("data-pipeup-id")===e)return r;return null}function We(t,e,r={}){let n=t.id?Ye(e,t.id):e;return t.quote?tt(t,t.quote,n,e,r.fuzzy!==!1):et(t,n)}function et(t,e){if(!e)return P;let r=e;if(t.path){if(!t.path.startsWith(Ve))return P;try{r=e.querySelector(t.path)}catch{return P}}return r?{state:A(r.textContent??"")===t.fingerprint?"attached":"moved",element:r,range:null}:P}function tt(t,e,r,n,o){let i=r??n,s=v(i),a=nt(s.text,e);if(a>=0){let d=H(s,a,a+e.exact.length);if(d){let c=C(d.startContainer,n);return{state:r!==null&&A(c.textContent??"")===t.fingerprint?"attached":"moved",element:c,range:d}}}if(o){let d=i===n?s:v(n);if(d.text.length<=Ze&&e.exact.length>=Qe&&e.exact.length<=Je){let c=Ae(d.text,e.exact,Math.floor(e.exact.length/4)),l=c?H(d,c.start,c.end):null;if(l)return{state:"moved",element:C(l.startContainer,n),range:l}}}return P}function nt(t,e){if(!e.exact)return-1;let r=-1,n=-1;for(let o=t.indexOf(e.exact);o!==-1;o=t.indexOf(e.exact,o+1)){let i=t.slice(Math.max(0,o-e.prefix.length),o),s=t.slice(o+e.exact.length,o+e.exact.length+e.suffix.length),a=ot(i,e.prefix)+rt(s,e.suffix);a>n&&(r=o,n=a)}return r}function rt(t,e){let r=0;for(;r<t.length&&r<e.length&&t[r]===e[r];)r++;return r}function ot(t,e){let r=0;for(;r<t.length&&r<e.length&&t[t.length-1-r]===e[e.length-1-r];)r++;return r}var it={A:"Link",BUTTON:"Button",P:"Paragraph",LI:"List item",IMG:"Image",TABLE:"Table",TD:"Cell",TH:"Cell",FIGURE:"Figure",SVG:"Graphic",CANVAS:"Graphic",LABEL:"Label",INPUT:"Field",SELECT:"Field",TEXTAREA:"Field",SUMMARY:"Question",BLOCKQUOTE:"Quote",SECTION:"Section",H1:"Heading",H2:"Heading",H3:"Heading",H4:"Heading",H5:"Heading",H6:"Heading"},st=40;function Oe(t){return t.getAttribute("data-pipeup-label")||it[t.tagName.toUpperCase()]||"Block"}function at(t){return X(y(t.getAttribute("aria-label")??t.getAttribute("alt")??t.innerText??t.textContent??""),st)}function Ee(t){let e=t.getAttribute("data-pipeup-label");if(e)return e;let r=at(t);return r?`${Oe(t)} \xB7 ${r}`:Oe(t)}function dt(t,e,r){let n=t.id??null,o=t.quote?.exact??null,i=t.point?`${Math.round(t.point.x*100)}% across, ${Math.round(t.point.y*100)}% down the element`:null,s=e.element;if(e.state==="orphaned"||!s)return{where:`No longer on the page (it read: "${t.snapshot}")`,element:null,id:n,quote:o,pin:i};let a=[],d=t.view?.slide;if(d){let l=s.closest("[data-pipeup-slide]")?.querySelector("h1,h2,h3")?.textContent;a.push(`Slide ${d}`+(l?` "${X(y(l))}"`:""))}else{let l=ct(t.view);l&&a.push(l);let Z=pt(s,r);Z&&a.push(`Section "${X(y(Z.textContent??""))}"`)}let c=Ee(s);return a.push(c),{where:a.join(" \u203A "),element:c,id:n,quote:o,pin:i}}function ct(t){return t?t.label||Object.entries(t).filter(([e,r])=>r&&e!=="slide").map(([,e])=>e).join(" \xB7 "):""}function pt(t,e){let r=null;for(let n of e.querySelectorAll("h1,h2,h3")){if(n===t||n.contains(t))break;if(n.compareDocumentPosition(t)&4)r=n;else break}return r}function X(t,e=60){return t.length>e?t.slice(0,e-1)+"\u2026":t}var M=["Otter","Fox","Owl","Bear","Rabbit"],G=["Red","Orange","Yellow","Green","Teal","Blue","Purple","Pink","Brown","Grey"];function ve(t){let e=2166136261;for(let r=0;r<t.length;r++)e=Math.imul(e^t.charCodeAt(r),16777619);return e=Math.imul(e^e>>>16,2246822507),e=Math.imul(e^e>>>13,3266489909),(e^e>>>16)>>>0}var lt=t=>ve(t)%M.length,ut=t=>Math.floor(ve(t)/M.length)%G.length,Pe=t=>`${G[ut(t)]} ${M[lt(t)]}`,b=t=>t.name||Pe(t.author);var ft="Each thread says where it is: the slide or section, the element (its data-pipeup-id attribute in the HTML), and the exact quoted text or pin position. Work through the open threads in order, and for each one say what you changed or why you did not.";function $(t,e){let r=Math.max(0,Math.round((e-t)/1e3));if(r<60)return"just now";let n=Math.round(r/60);if(n<60)return`${n}m ago`;let o=Math.round(n/60);return o<48?`${o}h ago`:`${Math.round(o/24)}d ago`}function mt(t,e,r,n){return e==="ai"?Se(t,"## Comment thread",n,r):ke(t)}function ht(t,e,r){let n=t.filter(a=>!a.thread.resolved);if(e==="text")return n.map(ke).join(`
|
|
3
|
-
`);let o=t.length-n.length,i=[`# Review comments: ${u(r.title)}`,"",`- **Page:** ${u(r.url)}`,`- **Exported:** ${r.exportedAt.toISOString()}`,`- **Open threads:** ${n.length}${o?` (${o} resolved, not included)`:""}`,"",ft,"",""].join(`
|
|
4
|
-
`),s=r.exportedAt.getTime();return i+n.map((a,d)=>Se(a,`## Thread ${d+1}`,s)).join(`
|
|
5
|
-
`)}function u(t){return t.replace(/\s+/g," ").trim()}function Se(t,e,r,n){let{thread:o,location:i}=t,s=o.root.replies.length,a=[`${e} \xB7 ${o.resolved?"resolved":"open"}`];if(a.push(`- **Thread:** ${o.id}`),n&&a.push(`- **Page:** ${u(n)}`),a.push(`- **Where:** ${u(i.where)}`),i.id){let d=u(i.id.replace(/`/g,""));a.push(`- **Element:** \`[data-pipeup-id="${d}"]\`${i.element?` (${u(i.element)})`:""}`)}i.quote&&a.push(`- **Quoted text:** "${u(i.quote)}"`),i.pin&&a.push(`- **Pin:** ${u(i.pin)}`),a.push(`- **Started by:** ${u(b(o.root))}, ${$(o.root.at,r)}${s?` \xB7 ${s} ${s===1?"reply":"replies"}`:""}`,"",`**${u(b(o.root))}** (${$(o.root.at,r)}): ${N(o.root," ")}`);for(let d of o.root.replies)a.push(`- **${u(b(d))}** (${$(d.at,r)}): ${N(d," ")} <!-- comment:${d.id} -->`);return a.join(`
|
|
6
|
-
`)+`
|
|
7
|
-
`}function ke(t){let{thread:e,location:r}=t,n=[u(r.where)+(r.quote?`, on the text "${u(r.quote)}"`:""),`${u(b(e.root))}: ${N(e.root," ")}`];for(let o of e.root.replies)n.push(` ${u(b(o))}: ${N(o," ")}`);return n.join(`
|
|
8
|
-
`)+`
|
|
9
|
-
`}function N(t,e){return t.deleted?"(deleted)":t.text.replace(/\r\n|\r|\n/g,`
|
|
10
|
-
`+e)+(t.edited?" (edited)":"")}var sn="0.5.1";export{M as ANIMALS,G as COLOURS,le as DEFAULT_PROFILE_NAME,j as IndexedDbStore,q as MemoryStore,K as PipeupDocument,E as UnsavedChangeError,sn as VERSION,Pe as animalName,ht as copyAll,mt as copyThread,R as createIdentity,Xe as describeElement,He as describeRange,$ as formatAgo,Ee as labelOf,Ke as loadOrCreateProfile,dt as locate,b as nameOf,Fe as newDocumentAttribute,De as parseDocumentAttribute,We as resolveAnchor};
|
|
1
|
+
function e(e){let t="";for(let r of e)t+=String.fromCharCode(r);return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function t(e){let t=atob(e.replace(/-/g,"+").replace(/_/g,"/")),r=new Uint8Array(t.length);for(let e=0;e<t.length;e++)r[e]=t.charCodeAt(e);return r}function r(e){return(new TextEncoder).encode(e)}function n(e){if(null===e||"object"!=typeof e)return JSON.stringify(e);if(Array.isArray(e))return"["+e.map(n).join(",")+"]";let t=e;return"{"+Object.keys(t).filter(e=>void 0!==t[e]).sort().map(e=>JSON.stringify(e)+":"+n(t[e])).join(",")+"}"}function i(e){return e.replace(/\s+/g," ").trim()}function o(e){let t=i(e),r=2166136261;for(let e=0;e<t.length;e++)r^=t.charCodeAt(e),r=Math.imul(r,16777619)>>>0;return r.toString(16).padStart(8,"0")}var a=/^[A-Za-z0-9_-]{43}$/,s=new Map;async function l(){let t=await crypto.subtle.generateKey({name:"Ed25519"},!1,["sign","verify"]);return{publicKey:e(new Uint8Array(await crypto.subtle.exportKey("raw",t.publicKey))),privateKey:t.privateKey}}async function u(t,r){let n=await crypto.subtle.sign({name:"Ed25519"},t.privateKey,r);return e(new Uint8Array(n))}var d=1e4,c=2**40,h=2**47,p=1e4,f=/^[0-9a-f]{8}$/,m=/^[A-Za-z0-9_-]{43}$/,y=/^[A-Za-z0-9_-]{43}$/,g=new Set(["create","reply","edit","delete","resolve","reopen"]);async function w(e,t){if(e.author!==t.publicKey)throw new Error("pipeup: an op must be signed by its author");return{body:e,sig:await u(t,r(n(e)))}}var b=(e,t)=>"string"==typeof e&&e.length<=t,x=e=>!!e&&"object"==typeof e,v=e=>"number"==typeof e&&Number.isFinite(e)&&e>=0&&e<=1;function A(e){if(!x(e))return!1;let{id:t,path:r,fingerprint:n,snapshot:i,quote:o,point:a,view:s}=e;if(!(b(r,2e3)&&b(i,500)&&"string"==typeof n&&f.test(n)&&(void 0===t||b(t,200)&&""!==t)&&(void 0===o||x(o)&&b(o.exact,p)&&""!==o.exact&&b(o.prefix,64)&&b(o.suffix,64))&&(void 0===a||x(a)&&v(a.x)&&v(a.y))))return!1;if(void 0!==s){if(!x(s)||Array.isArray(s))return!1;let e=Object.entries(s);if(e.length>16||!e.every(([e,t])=>e.length<=64&&b(t,200)))return!1}return!0}function E(e){if(!x(e))return!1;let{body:t,sig:r}=e;if("string"!=typeof r||!x(t)||JSON.stringify({body:t,sig:r}).length>262144)return!1;let n=t;return!(1!==n.v||"string"!=typeof n.id||!m.test(n.id)||!g.has(n.kind)||!b(n.doc,64)||""===n.doc||"string"!=typeof n.thread||!m.test(n.thread)||"string"!=typeof n.author||!y.test(n.author)||"string"!=typeof n.name||n.name.length>80||n.name!==n.name.trim()||"number"!=typeof n.clock||!Number.isSafeInteger(n.clock)||n.clock<1||n.clock>c||"number"!=typeof n.at||!Number.isSafeInteger(n.at)||n.at<0||n.at>h||void 0!==n.text&&(!b(n.text,d)||n.text!==n.text.trim())||void 0!==n.target&&("string"!=typeof n.target||!m.test(n.target))||("create"===n.kind||"reply"===n.kind||"edit"===n.kind)&&!n.text||("reply"===n.kind||"edit"===n.kind||"delete"===n.kind)&&!n.target||"create"===n.kind&&(n.id!==n.thread||!A(n.anchor)))}function k(i){return async function(r,n,i){if(!a.test(r))return!1;try{if(e(t(r))!==r)return!1;let o=s.get(r);if(!o){s.size>=512&&s.delete(s.keys().next().value),o=crypto.subtle.importKey("raw",t(r),{name:"Ed25519"},!0,["verify"]),s.set(r,o);let e=o;e.catch(()=>{s.get(r)===e&&s.delete(r)})}return await crypto.subtle.verify({name:"Ed25519"},await o,t(i),n)}catch{return!1}}(i.body.author,r(n(i.body)),i.sig)}function $(e,t){return e.body.clock!==t.body.clock?e.body.clock-t.body.clock:e.body.at!==t.body.at?e.body.at-t.body.at:e.body.author!==t.body.author?e.body.author<t.body.author?-1:1:e.body.id!==t.body.id?e.body.id<t.body.id?-1:1:0}function M(e,t,r,n,i){return{id:e,author:t,name:r,text:n,at:i,edited:!1,deleted:!1,replies:[]}}async function S(t){let i={...t,id:void 0,thread:"create"===t.kind?void 0:t.thread},o=await crypto.subtle.digest("SHA-256",r(n(i)));return e(new Uint8Array(o))}var C=class{constructor(e){this.doc=e}ops=new Map;clock=0;createAt=new Map;threadAt=new Map;get size(){return this.ops.size}all(){return[...this.ops.values()]}threads(){return function(e){let t=new Map,r=new Map,n=new Map;for(let{body:i}of[...e].sort($)){if(i.name&&n.set(i.author,i.name),"create"===i.kind){if(t.has(i.id)||!i.anchor||void 0===i.text)continue;let e=M(i.id,i.author,i.name,i.text,i.at);t.set(i.id,{id:i.id,anchor:i.anchor,root:e,resolved:!1}),r.set(i.id,{comment:e,thread:i.id});continue}let e=t.get(i.thread);if(!e)continue;if("resolve"===i.kind||"reopen"===i.kind){e.resolved="resolve"===i.kind;continue}let o=i.target?r.get(i.target):void 0;if(o&&o.thread===i.thread){if("reply"===i.kind){if(r.has(i.id)||void 0===i.text)continue;let t=M(i.id,i.author,i.name,i.text,i.at);e.root.replies.push(t),r.set(i.id,{comment:t,thread:i.thread});continue}i.author!==o.comment.author||o.comment.deleted||("edit"===i.kind&&void 0!==i.text&&(o.comment.text=i.text,o.comment.edited=!0),"delete"===i.kind&&(o.comment.text="",o.comment.deleted=!0))}}for(let{comment:e}of r.values())e.name||(e.unnamed=!0,e.name=n.get(e.author)??"");return[...t.values()]}(this.ops.values())}async add(e){let t=[];for(let r of e){if(!E(r)||r.body.doc!==this.doc||this.ops.has(r.body.id)||r.body.id!==await S(r.body)||!await k(r))continue;let e={body:r.body,sig:r.sig};this.ops.set(e.body.id,e),this.clock=Math.max(this.clock,e.body.clock),"create"===e.body.kind&&this.createAt.set(e.body.author,Math.max(this.createAt.get(e.body.author)??0,e.body.at)),this.threadAt.set(e.body.thread,Math.max(this.threadAt.get(e.body.thread)??0,e.body.at)),t.push(e)}return t}async append(e,t,r){let n={v:1,...r,id:"",thread:"create"===r.kind?"":r.thread,doc:this.doc,author:e.publicKey,name:t,clock:Math.min(this.clock+1,c),at:Math.min(Math.max(Date.now(),"create"===r.kind?(this.createAt.get(e.publicKey)??0)+1:(this.threadAt.get(r.thread)??0)+1),h)};n.id=await S(n);let i=this.ops.get(n.id);if(i)return i;"create"===n.kind&&(n.thread=n.id);let[o]=await this.add([await w(n,e)]);if(!o)throw new Error("pipeup: could not record this change");return o}},N=["Otter","Fox","Owl","Bear","Rabbit"],O=["Red","Orange","Yellow","Green","Teal","Blue","Purple","Pink","Brown","Grey"];function q(e){let t=2166136261;for(let r=0;r<e.length;r++)t=Math.imul(t^e.charCodeAt(r),16777619);return t=Math.imul(t^t>>>16,2246822507),t=Math.imul(t^t>>>13,3266489909),(t^t>>>16)>>>0}var P=e=>`${O[(e=>Math.floor(q(e)/N.length)%O.length)(e)]} ${N[(e=>q(e)%N.length)(e)]}`,T=e=>e.name||P(e.author),j="AI assistant",I=e=>e===j||e.startsWith(`${j} `),D=/^([A-Za-z0-9_-]{16}):([A-Za-z0-9_-]{43})$/;function F(){return`${function(t=16){return e(crypto.getRandomValues(new Uint8Array(t)))}(12)}:${e(crypto.getRandomValues(new Uint8Array(32)))}`}async function U(e){let r=D.exec(e.trim()),n=r?.[1],i=r?.[2];if(!n||!i)throw new Error('pipeup: data-pipeup-doc must look like "<id>:<key>", as made by newDocumentAttribute()');return{id:n,key:await crypto.subtle.importKey("raw",t(i),{name:"AES-GCM"},!1,["encrypt","decrypt"])}}async function R(t,n,i){let o=crypto.getRandomValues(new Uint8Array(12)),a=await crypto.subtle.encrypt({name:"AES-GCM",iv:o,additionalData:r(i)},t,r(n));return{iv:e(o),data:e(new Uint8Array(a))}}var H=e=>`pipeup-file:${e}`;var L=/^[a-z][a-z0-9-]{1,23}\/[a-z][a-z0-9-]{0,31}$/,z=class extends Error{constructor(e,t,r=1){super("pipeup: shown here, but not saved yet"),this.id=e,this.cause=t,this.count=r,this.name="UnsavedChangeError"}},K=class e{constructor(e,t){this.log=e,this.options=t,this.displayName=t.name.trim()?B(t.name):""}listeners=new Set;cache=[];comments=new Map;displayName;e=Promise.resolve();unsaved=[];static async open(t){if("string"!=typeof t.doc||""===t.doc||t.doc.length>64)throw new Error("pipeup: the document id must be 1 to 64 characters");let r=new C(t.doc),n=await t.store.load(t.doc),i=n.length-(await r.add(n)).length;i&&console.warn(`pipeup: ${i} saved change${1===i?"":"s"} on this page couldn't be read; kept as saved`);let o=new e(r,t);return o.refresh(!1),o}get id(){return this.options.doc}get me(){return this.options.identity.publicKey}get name(){return this.displayName}set name(e){this.displayName=B(e),this.refresh(!0,[],"")}threads(){return this.cache}ops(){return this.log.all()}onChange(e){return this.listeners.add(e),()=>this.listeners.delete(e)}comment(e,t){return this.enqueue(()=>this.write({kind:"create",anchor:e,text:_(t)}))}reply(e,t){return this.enqueue(()=>this.write({kind:"reply",thread:this.find(e).thread,target:e,text:_(t)}))}edit(e,t){return this.enqueue(async()=>{let{thread:r}=this.mine(e);await this.write({kind:"edit",thread:r,target:e,text:_(t)})})}remove(e){return this.enqueue(async()=>{let{thread:t}=this.mine(e);await this.write({kind:"delete",thread:t,target:e})})}resolve(e){return this.enqueue(async()=>{await this.write({kind:"resolve",thread:this.thread(e)})})}reopen(e){return this.enqueue(async()=>{await this.write({kind:"reopen",thread:this.thread(e)})})}sign(e,t){if(!L.test(e))throw new Error('pipeup: a signing purpose looks like "<add-on id>/<name>"');return u(this.options.identity,r(`pipeup:${e}\n${t}`))}flush(){return this.enqueue(()=>this.persist())}exportFile(){return async function(e,t,r){let n=r?{pipeup:1,doc:e,sealed:!0,...await R(r,JSON.stringify(t),H(e))}:{pipeup:1,doc:e,sealed:!1,ops:t};return JSON.stringify(n,null,2)}(this.options.doc,this.log.all(),this.options.key)}async importFile(e){return this.merge(await async function(e,n,i){if(e.length>5e6)throw new Error("pipeup: this feedback file is too large");let o,a,s;try{o=JSON.parse(e)}catch{throw new Error("pipeup: this is not an Pipeup feedback file")}if(!o||"object"!=typeof o||1!==o.pipeup||"string"!=typeof o.doc)throw new Error("pipeup: this is not an Pipeup feedback file");if(o.doc!==n)throw new Error("pipeup: this feedback is for a different document");if(!o.sealed)return Array.isArray(o.ops)?o.ops:[];if(!i)throw new Error("pipeup: this feedback is sealed, and this page has no document key");if("string"!=typeof o.iv||"string"!=typeof o.data)throw new Error("pipeup: this is not an Pipeup feedback file");try{a=await async function(e,n,i){try{let o=await crypto.subtle.decrypt({name:"AES-GCM",iv:t(n.iv),additionalData:r(i)},e,t(n.data));return(new TextDecoder).decode(o)}catch{throw new Error("pipeup: this feedback could not be opened with this document")}}(i,{iv:o.iv,data:o.data},H(n))}catch{throw new Error("pipeup: this feedback could not be opened with this document's key")}try{s=JSON.parse(a)}catch{throw new Error("pipeup: this is not an Pipeup feedback file")}return Array.isArray(s)?s:[]}(e,this.options.doc,this.options.key),"file")}merge(e,t){return e.length>5e3?Promise.reject(new RangeError("pipeup: too many changes to merge at once")):this.enqueue(async()=>{let r=await this.log.add([...e]);this.unsaved.push(...r),r.length>0&&this.refresh(!0,r,t);try{await this.persist()}catch(e){throw r.length>0?new z(r[0].body.id,e,r.length):e}return r.length})}enqueue(e){let t=this.e.then(e);return this.e=t.catch(()=>{}),t}async write(e){let t=await this.log.append(this.options.identity,this.displayName,e);this.unsaved.push(t),this.refresh(!0,[t],"local");try{await this.persist()}catch(e){throw new z(t.body.id,e)}return t.body.id}async persist(){if(0===this.unsaved.length)return;let e=[...this.unsaved];await this.options.store.append(this.options.doc,e),this.unsaved=this.unsaved.filter(t=>!e.includes(t))}refresh(e,t=[],r=""){this.cache=this.log.threads(),this.comments.clear();let n=(e,t)=>{e.unnamed&&e.author===this.me&&this.displayName&&(e.name=this.displayName),this.comments.set(e.id,{comment:e,thread:t});for(let r of e.replies)n(r,t)};for(let e of this.cache)n(e.root,e.id);if(e)for(let e of this.listeners)try{e(this.cache,t,r)}catch(e){globalThis.reportError?.(e)}}find(e){let t=this.comments.get(e);if(!t)throw new Error("pipeup: there is no comment with that id");return t}mine(e){let t=this.find(e);if(t.comment.author!==this.me)throw new Error("pipeup: only the writer can change a comment");return t}thread(e){if(!this.cache.some(t=>t.id===e))throw new Error("pipeup: there is no thread with that id");return e}};function _(e){let t=e.trim();if(!t)throw new Error("pipeup: a comment needs some words");if(t.length>d)throw new Error(`pipeup: comments are limited to ${d} characters`);return t}function B(e){let t=e.replace(/\s+/g," ").trim();if(!t||t.length>80)throw new Error("pipeup: choose a name of 1 to 80 characters");if(I(t))throw new Error("pipeup: that name is kept for AI replies; choose another");return t}var G=class{docs=new Map;profile=null;async load(e){return[...this.docs.get(e)?.values()??[]]}async append(e,t){let r=this.docs.get(e);r||this.docs.set(e,r=new Map);for(let e of t)r.set(e.body.id,e)}async loadProfile(){return this.profile}async saveProfile(e){this.profile=e}async saveProfileIfAbsent(e){return this.profile||(this.profile=e),this.profile}},J="";async function V(e,t=""){let r=await e.loadProfile();if(r)return"Reviewer"===r.name?{...r,name:""}:r;let n={identity:await l(),name:t};return e.saveProfileIfAbsent(n)}var W=class e{constructor(e){this.db=e}static open(t="pipeup"){return new Promise((r,n)=>{let i=indexedDB.open(t,1);i.onupgradeneeded=()=>{let e=i.result;e.createObjectStore("ops",{keyPath:["doc","id"]}).createIndex("doc","doc"),e.createObjectStore("meta")},i.onsuccess=()=>{let t=i.result;t.onversionchange=()=>t.close(),r(new e(t))},i.onerror=()=>n(i.error??new Error("pipeup: could not open local storage"))})}close(){this.db.close()}async load(e){return(await this.request("ops","readonly",t=>t.index("doc").getAll(e))).map(e=>e.op)}async append(e,t){if(0===t.length)return;let r=this.db.transaction("ops","readwrite"),n=r.objectStore("ops"),i=Z(r);try{for(let r of t)n.put({doc:e,id:r.body.id,op:r})}catch(e){throw r.abort(),i.catch(()=>{}),e}await i}async loadProfile(){return await this.request("meta","readonly",e=>e.get("profile"))??null}async saveProfile(e){await this.request("meta","readwrite",t=>t.put(e,"profile"))}async saveProfileIfAbsent(e){let t=this.db.transaction("meta","readwrite"),r=t.objectStore("meta"),n=Z(t),i=e,o=r.get("profile");return o.onsuccess=()=>{o.result?i=o.result:r.put(e,"profile")},await n,i}request(e,t,r){return new Promise((n,i)=>{let o=r(this.db.transaction(e,t).objectStore(e));o.onsuccess=()=>n(o.result),o.onerror=()=>i(o.error??new Error("pipeup: local storage request failed"))})}};function Z(e){return new Promise((t,r)=>{e.oncomplete=()=>t(),e.onerror=()=>r(e.error??new Error("pipeup: local storage write failed")),e.onabort=()=>r(e.error??new Error("pipeup: local storage write aborted"))})}function Q(e){let t=(e.ownerDocument??e).createTreeWalker(e,NodeFilter.SHOW_TEXT),r=[],n="";for(let e=t.nextNode();e;e=t.nextNode())r.push({node:e,start:n.length}),n+=e.data;return{text:n,nodes:r}}function X(e,t,r){let n=(e.ownerDocument??e).createRange();return n.setStart(e,0),n.setEnd(t,r),n.toString().length}function Y(e,t,r){let n=ee(e,t,!1),i=ee(e,r,!0);if(!n||!i||r<t)return null;let o=n.node.ownerDocument.createRange();return o.setStart(n.node,n.offset),o.setEnd(i.node,i.offset),o}function ee(e,t,r){let n=e.nodes.find(e=>t>=e.start&&t<e.start+e.node.data.length+Number(r));return n&&{node:n.node,offset:t-n.start}}function te(e){return e.nodeType===Node.ELEMENT_NODE?e:e.parentElement}function re(e,t){for(let r=te(e)?.closest("[data-pipeup-id]")??null;r;){if(!t.contains(r))return null;let e=r.getAttribute("data-pipeup-id")??"";if(""!==e&&e.length<=200)return r;r=r.parentElement?.closest("[data-pipeup-id]")??null}return null}function ne(e,t,r){return e.slice(t,r).replace(/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/g,"")}function ie(e,t){let r=function(e,t){if(e===t)return"";let r=[];for(let n=t;n&&n!==e;n=n.parentElement){let e=1;for(let t=n.previousElementSibling;t;t=t.previousElementSibling)t.tagName===n.tagName&&e++;r.unshift(`${n.tagName.toLowerCase()}:nth-of-type(${e})`)}return":scope > "+r.join(" > ")}(e,t);if(r.length>2e3)throw new Error("pipeup: this element is nested too deeply to comment on — give a nearby container a data-pipeup-id");return r}function oe(e){if(!A(e))throw new Error("pipeup: could not describe this place on the page");return e}function ae(e,t){let r=te(e),n=r?.closest("p,li,h1,h2,h3,h4,h5,h6,td,th,blockquote,pre,figcaption,dt,dd,summary,caption")??null;return n&&t.contains(n)?n:r&&t.contains(r)?r:t}function se(e,t,r){if(!t.contains(e.commonAncestorContainer))throw new Error("pipeup: the selection is outside the commentable area");let n=re(e.commonAncestorContainer,t),a=n??t,{text:s}=Q(a),l=X(a,e.startContainer,e.startOffset),u=X(a,e.endContainer,e.endOffset),d=s.slice(l,u);if(""===d)throw new Error("pipeup: select some text to comment on");if(d.length>p)throw new Error("pipeup: selections are limited to 10000 characters");let c=ne(s,Math.max(0,l-32),l),h=ne(s,u,u+32);return oe({...n?{id:n.getAttribute("data-pipeup-id")??""}:{},path:"",quote:{exact:d,prefix:c,suffix:h},...r?{view:r}:{},fingerprint:o(ae(e.startContainer,t).textContent??""),snapshot:ne(i(c+d+h),0,200)})}function le(e,t,r,n){let a=re(e,t);return oe({...a?{id:a.getAttribute("data-pipeup-id")??""}:{},path:ie(a??t,e),...r?{point:{x:ue(r.x),y:ue(r.y)}}:{},...n?{view:n}:{},fingerprint:o(e.textContent??""),snapshot:ne(i(e.textContent??""),0,200)})}function ue(e){return Number.isFinite(e)?Math.min(1,Math.max(0,e)):0}function de(e,t,r){let n=Array.from({length:t.length+1},(e,t)=>t),i=[...n],o=[t.length];for(let a=1;a<=e.length;a++){i[0]=r?0:a;for(let r=1;r<=t.length;r++){let o=e.charCodeAt(a-1)===t.charCodeAt(r-1)?0:1;i[r]=Math.min(n[r]+1,i[r-1]+1,n[r-1]+o)}o.push(i[t.length]),[n,i]=[i,n]}return o}var ce=(e,t)=>de(e,t,!1).pop(),he={state:"orphaned",element:null,range:null};function pe(e,t,r={}){let n=e.id?function(e,t){for(let r of e.querySelectorAll("[data-pipeup-id]"))if(r.getAttribute("data-pipeup-id")===t)return r;return null}(t,e.id):t;return e.quote?function(e,t,r,n,i){let a=r??n,s=Q(a),l=function(e,t){if(!t.exact)return-1;let r=-1,n=-1;for(let i=e.indexOf(t.exact);-1!==i;i=e.indexOf(t.exact,i+1)){let o=e.slice(Math.max(0,i-t.prefix.length),i),a=e.slice(i+t.exact.length,i+t.exact.length+t.suffix.length),s=me(o,t.prefix)+fe(a,t.suffix);s>n&&(r=i,n=s)}return r}(s.text,t);if(l>=0){let i=Y(s,l,l+t.exact.length);if(i){let t=ae(i.startContainer,n);return{state:null!==r&&o(t.textContent??"")===e.fingerprint?"attached":"moved",element:t,range:i}}}if(i){let e=a===n?s:Q(n);if(e.text.length<=5e4&&t.exact.length>=12&&t.exact.length<=300){let r=function(e,t,r){let n=t.length;if(0===n)return null;let i=-1,o=1/0;if(de(e,t,!0).forEach((e,t)=>{t&&e<o&&(o=e,i=t)}),o>r)return null;let a=Math.max(0,i-n),s=1/0;for(let o=Math.max(0,i-n-r);o<=Math.min(i,i-n+r);o++){let r=ce(e.slice(o,i),t);r<s&&(s=r,a=o)}return{start:a,end:i,distance:s}}(e.text,t.exact,Math.floor(t.exact.length/4)),i=r?Y(e,r.start,r.end):null;if(i)return{state:"moved",element:ae(i.startContainer,n),range:i}}}return he}(e,e.quote,n,t,!1!==r.fuzzy):function(e,t){if(!t)return he;let r=t;if(e.path){if(!e.path.startsWith(":scope > "))return he;try{r=t.querySelector(e.path)}catch{return he}}return r?{state:o(r.textContent??"")===e.fingerprint?"attached":"moved",element:r,range:null}:he}(e,n)}function fe(e,t){let r=0;for(;r<e.length&&r<t.length&&e[r]===t[r];)r++;return r}function me(e,t){let r=0;for(;r<e.length&&r<t.length&&e[e.length-1-r]===t[t.length-1-r];)r++;return r}var ye={A:"Link",BUTTON:"Button",P:"Paragraph",LI:"List item",IMG:"Image",TABLE:"Table",TD:"Cell",TH:"Cell",FIGURE:"Figure",SVG:"Graphic",CANVAS:"Graphic",LABEL:"Label",INPUT:"Field",SELECT:"Field",TEXTAREA:"Field",SUMMARY:"Question",BLOCKQUOTE:"Quote",SECTION:"Section",H1:"Heading",H2:"Heading",H3:"Heading",H4:"Heading",H5:"Heading",H6:"Heading"};function ge(e){return e.getAttribute("data-pipeup-label")||ye[e.tagName.toUpperCase()]||"Block"}function we(e){let t=e.getAttribute("data-pipeup-label");if(t)return t;let r=function(e){return xe(i(e.getAttribute("aria-label")??e.getAttribute("alt")??e.innerText??e.textContent??""),40)}(e);return r?`${ge(e)} · ${r}`:ge(e)}function be(e,t,r){let n=e.id??null,o=e.quote?.exact??null,a=e.point?`${Math.round(100*e.point.x)}% across, ${Math.round(100*e.point.y)}% down the element`:null,s=t.element;if("orphaned"===t.state||!s)return{where:`No longer on the page (it read: "${e.snapshot}")`,element:null,id:n,quote:o,pin:a};let l=[],u=e.view?.slide;if(u){let e=s.closest("[data-pipeup-slide]")?.querySelector("h1,h2,h3")?.textContent;l.push(`Slide ${u}`+(e?` "${xe(i(e))}"`:""))}else{let t=function(e){return e?e.label||Object.entries(e).filter(([e,t])=>t&&"slide"!==e).map(([,e])=>e).join(" · "):""}(e.view);t&&l.push(t);let n=function(e,t){let r=null;for(let n of t.querySelectorAll("h1,h2,h3")){if(n===e||n.contains(e))break;if(!(4&n.compareDocumentPosition(e)))break;r=n}return r}(s,r);n&&l.push(`Section "${xe(i(n.textContent??""))}"`)}let d=we(s);return l.push(d),{where:l.join(" › "),element:d,id:n,quote:o,pin:a}}function xe(e,t=60){return e.length>t?e.slice(0,t-1)+"…":e}var ve="Each thread says where it is: the slide or section, the element (its data-pipeup-id attribute in the HTML), and the exact quoted text or pin position. Work through the open threads in order, and for each one say what you changed or why you did not.";function Ae(e,t){let r=Math.max(0,Math.round((t-e)/1e3));if(r<60)return"just now";let n=Math.round(r/60);if(n<60)return`${n}m ago`;let i=Math.round(n/60);return i<48?`${i}h ago`:`${Math.round(i/24)}d ago`}function Ee(e,t,r,n){return"ai"===t?Me(e,"## Comment thread",n,r):Se(e)}function ke(e,t,r){let n=e.filter(e=>!e.thread.resolved);if("text"===t)return n.map(Se).join("\n");let i=e.length-n.length,o=[`# Review comments: ${$e(r.title)}`,"",`- **Page:** ${$e(r.url)}`,`- **Exported:** ${r.exportedAt.toISOString()}`,`- **Open threads:** ${n.length}${i?` (${i} resolved, not included)`:""}`,"",ve,"",""].join("\n"),a=r.exportedAt.getTime();return o+n.map((e,t)=>Me(e,`## Thread ${t+1}`,a)).join("\n")}function $e(e){return e.replace(/\s+/g," ").trim()}function Me(e,t,r,n){let{thread:i,location:o}=e,a=i.root.replies.length,s=[`${t} · ${i.resolved?"resolved":"open"}`];if(s.push(`- **Thread:** ${i.id}`),n&&s.push(`- **Page:** ${$e(n)}`),s.push(`- **Where:** ${$e(o.where)}`),o.id){let e=$e(o.id.replace(/`/g,""));s.push(`- **Element:** \`[data-pipeup-id="${e}"]\`${o.element?` (${$e(o.element)})`:""}`)}o.quote&&s.push(`- **Quoted text:** "${$e(o.quote)}"`),o.pin&&s.push(`- **Pin:** ${$e(o.pin)}`),s.push(`- **Started by:** ${$e(T(i.root))}, ${Ae(i.root.at,r)}${a?` · ${a} ${1===a?"reply":"replies"}`:""}`,"",`**${$e(T(i.root))}** (${Ae(i.root.at,r)}): ${Ce(i.root," ")}`);for(let e of i.root.replies)s.push(`- **${$e(T(e))}** (${Ae(e.at,r)}): ${Ce(e," ")} \x3c!-- comment:${e.id} --\x3e`);return s.join("\n")+"\n"}function Se(e){let{thread:t,location:r}=e,n=[$e(r.where)+(r.quote?`, on the text "${$e(r.quote)}"`:""),`${$e(T(t.root))}: ${Ce(t.root," ")}`];for(let e of t.root.replies)n.push(` ${$e(T(e))}: ${Ce(e," ")}`);return n.join("\n")+"\n"}function Ce(e,t){return e.deleted?"(deleted)":e.text.replace(/\r\n|\r|\n/g,"\n"+t)+(e.edited?" (edited)":"")}var Ne="0.5.3";export{j as AI_NAME,N as ANIMALS,O as COLOURS,J as DEFAULT_PROFILE_NAME,W as IndexedDbStore,G as MemoryStore,K as PipeupDocument,z as UnsavedChangeError,Ne as VERSION,P as animalName,S as computeOpId,ke as copyAll,Ee as copyThread,l as createIdentity,le as describeElement,se as describeRange,Ae as formatAgo,I as isAiName,we as labelOf,V as loadOrCreateProfile,be as locate,T as nameOf,F as newDocumentAttribute,U as parseDocumentAttribute,pe as resolveAnchor,w as signOp};
|