hyper-undo 0.7.0 → 0.7.1
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/CHANGELOG.md +5 -0
- package/dist/hyper-undo.min.js +1 -1
- package/package.json +1 -1
- package/src/replay-plan.js +8 -4
- package/src/scope.js +25 -1
package/CHANGELOG.md
CHANGED
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
|
|
20
20
|
## [Unreleased]
|
|
21
21
|
|
|
22
|
+
## [0.7.1] - 2026-10-30
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
- Undo and redo keep changes made inside a subtree that an edit removes: a child moved out of a column that is then removed returns to the column on undo and leaves again on redo.
|
|
26
|
+
|
|
22
27
|
## [0.7.0] - 2026-10-30
|
|
23
28
|
|
|
24
29
|
### Added
|
package/dist/hyper-undo.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var hyperundo=(()=>{var J=Object.defineProperty;var ct=Object.getOwnPropertyDescriptor;var ft=Object.getOwnPropertyNames;var mt=Object.prototype.hasOwnProperty;var pt=(e,n)=>{for(var t in n)J(e,t,{get:n[t],enumerable:!0})},gt=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of ft(n))!mt.call(e,i)&&i!==t&&J(e,i,{get:()=>n[i],enumerable:!(r=ct(n,i))||r.enumerable});return e};var ht=e=>gt(J({},"__esModule",{value:!0}),e);var Yt={};pt(Yt,{default:()=>Gt,undo:()=>ne});function yt(e,n=null){let t=[];if(e.type==="attributes"){let r=e.target,i=e.attributeName,a=e.oldValue,l=r.getAttribute(i);return a==null&&l!=null?t.push({kind:"attr-add",target:r,name:i,newValue:l}):a!=null&&l==null?t.push({kind:"attr-remove",target:r,name:i,oldValue:a}):a!==l&&t.push({kind:"attr-set",target:r,name:i,oldValue:a,newValue:l}),t}if(e.type==="characterData")return t.push({kind:"text",target:e.target,oldValue:e.oldValue,newValue:e.target.data}),t;if(e.type==="childList"){let r=e.target,i=e.nextSibling,a=e.nextSibling;for(;a&&n?.(a);)a=a.nextSibling;let l=e.previousSibling;for(;l&&n?.(l);)l=l.previousSibling;let s=n?Array.from(e.addedNodes).filter(g=>!n(g)):Array.from(e.addedNodes),d=n?Array.from(e.removedNodes).filter(g=>!n(g)):Array.from(e.removedNodes);return s.length>0&&t.push({kind:"add",parent:r,nodes:s,before:i,afterAuthored:a,beforeAuthored:l,record:e}),d.length>0&&t.push({kind:"remove",parent:r,nodes:d,before:i,afterAuthored:a,beforeAuthored:l,record:e}),t}return t}function ve(e,n=null){let t=[],r=new Map;for(let i of e){if(i.type==="attributes"){let a=i.target,l=i.attributeName,s=r.get(a);s||(s=new Map,r.set(a,s)),s.has(l)||(s.set(l,{idx:t.length,oldValue:i.oldValue}),t.push(null));continue}for(let a of yt(i,n))t.push(a)}for(let[i,a]of r)for(let[l,s]of a){let d=s.oldValue,g=i.getAttribute(l),p=null;d===g?p=null:d==null?p={kind:"attr-add",target:i,name:l,newValue:g}:g==null?p={kind:"attr-remove",target:i,name:l,oldValue:d}:p={kind:"attr-set",target:i,name:l,oldValue:d,newValue:g},t[s.idx]=p}return t.filter(i=>i!=null)}function Ae(e){let n=[],t=new Map;for(let i of e){let a=we(i);if(a==null){n.push(i);continue}let l=t.get(i.target);l||(l=new Map,t.set(i.target,l));let s=l.get(a);if(s==null){let d=ke(i);if(d.oldValue===d.newValue)continue;l.set(a,n.length),n.push({...i,...d})}else n[s]={...n[s],newValue:ke(i).newValue}}let r=[];for(let i of n){if(we(i)==null){r.push(i);continue}i.oldValue!==i.newValue&&r.push(wt(i))}return r}var bt=new Set(["attr-set","attr-add","attr-remove","text","value"]);function we(e){return bt.has(e.kind)?e.kind==="text"?"#text":e.kind==="value"?`#${e.prop||"value"}`:e.name==null?null:`@${e.name}`:null}function ke(e){return e.kind==="attr-add"?{oldValue:null,newValue:e.newValue}:e.kind==="attr-remove"?{oldValue:e.oldValue,newValue:null}:{oldValue:e.oldValue,newValue:e.newValue}}function wt(e){return e.kind!=="attr-add"&&e.kind!=="attr-remove"?e:e.oldValue==null&&e.newValue!=null?{...e,kind:"attr-add"}:e.oldValue!=null&&e.newValue==null?{...e,kind:"attr-remove"}:{...e,kind:"attr-set"}}var Ne=new WeakMap,kt=0;function T(e){let n=Ne.get(e);return n||(n=`n${++kt}`,Ne.set(e,n)),n}function v(e){return!!e&&typeof e=="object"&&typeof e.nodeType=="number"}function vt(e){return e.kind==="add"||e.kind==="remove"}function Se(e,n){let t=[],r=e.childNodes;if(!r)return t;for(let i of r)n&&n(i)||t.push(T(i));return t}function z(e,n,t=null,r=null){if(!v(e))return null;if(e.nodeType===3)return`#text:${JSON.stringify(e.data)}`;if(e.nodeType===8)return`#comment:${JSON.stringify(e.data)}`;if(e.nodeType!==1)return`#node:${e.nodeType}`;let i=[];if(e.attributes){for(let s of e.attributes)At(s.name,e,n,t)||i.push(`${s.name}=${JSON.stringify(s.value)}`);i.sort()}let a=[];if(e.childNodes)for(let s of e.childNodes)Nt(s,r)||a.push(z(s,n,t,r));return`<${String(e.tagName||"").toLowerCase()}${i.length?` ${i.join(" ")}`:""}>${a.join("")}</>`}function G(e,n=[]){if(!v(e))return n;if(e.nodeType===1){let t=e.getAttribute&&e.getAttribute("data-id");t&&n.push(t)}if(e.childNodes)for(let t of e.childNodes)G(t,n);return n}function At(e,n,t,r=null){if(typeof t!="function")return!1;try{return!!t(e,n,r)}catch{return!1}}function Nt(e,n){if(typeof n!="function")return!1;try{return!!n(e)}catch{return!1}}function Te(e,n){if(typeof n!="function")return[];let t=[],r=i=>{if(!v(i))return;let a=!1;try{a=!!n(i)}catch{a=!1}if(a&&t.push(E(i,"protected")),i.childNodes)for(let l of i.childNodes)r(l)};for(let i of e)r(i);return t}function E(e,n,t=null){let r=e;r&&r.nodeType!==1&&(r=r.parentElement);let i=r&&typeof r.closest=="function"?r.closest("[data-id]"):null;return!i&&t&&typeof t.closest=="function"&&(i=t.closest("[data-id]")),{id:(i?i.getAttribute("data-id"):null)||null,kind:n||null}}function Et(e,n,t=null){return(e||[]).filter(v).map(r=>E(r,n,t))}function q(e){return{replayable:!1,reason:e,steps:[]}}function D(e,n){let t=[[n.afterAuthored,i=>i],[n.beforeAuthored,i=>i+1],[n.before,i=>i]],r=!1;for(let[i,a]of t){if(!v(i))continue;r=!0;let l=e.indexOf(T(i));if(l>=0)return a(l)}return r?null:e.length}function Ve(e,n={}){let t=n.isIgnored||null,r=n.ignoreAttribute||null,i=Ae(e),a=[];for(let p of i){if(!vt(p)){if(!v(p.target))return q("non-dom-target");continue}if(!v(p.parent))return q("non-dom-parent");let h=Array.from(p.nodes||[]).filter(v);if(h.length!==0){if(h.length!==Array.from(p.nodes||[]).length)return q("non-dom-node");a.push({primitive:p,nodes:h,parent:p.parent})}}if(St(a))return q("in-record-move");let l=Tt(a,t);if(!l)return q("ambiguous-structure");let s=Vt(a,l),d=new Map(a.map(p=>[p.primitive,p])),g=[];for(let p of i){let h=d.get(p);if(!h){g.push(Pt(p));continue}let w=s.get(h);if(w){w.at===h&&g.push(w.step);continue}g.push(It(h,l.get(h),r,t))}return{replayable:!0,reason:null,steps:g}}function St(e){let n=new Map;for(let t of e){let r=t.primitive.record;if(!r)continue;let i=n.get(r);i||(i=new Map,n.set(r,i));for(let a of t.nodes){let l=i.get(a);l||(l=new Set,i.set(a,l)),l.add(t.primitive.kind)}}for(let t of n.values())for(let r of t.values())if(r.size>1)return!0;return!1}function Tt(e,n){let t=new Map;for(let i of e)t.has(i.parent)||t.set(i.parent,Se(i.parent,n));let r=new Map;for(let i=e.length-1;i>=0;i--){let a=e[i],l=t.get(a.parent);if(!l)return null;let s=l.slice(),d;if(a.primitive.kind==="add"){let g=a.nodes.map(T);if(!g.every(p=>l.includes(p)))return null;d=l.filter(p=>!g.includes(p))}else{let g=D(s,a.primitive);if(g==null)return null;d=s.slice(),d.splice(g,0,...a.nodes.map(T))}r.set(a,{before:d,after:s}),t.set(a.parent,d)}return r}function Vt(e,n){let t=new Map,r=new Map;e.forEach((a,l)=>{for(let s of a.nodes){let d=T(s);a.primitive.kind==="remove"?t.set(d,{item:a,index:l,node:s}):r.set(d,{item:a,index:l,node:s})}});let i=new Map;for(let[a,l]of t){let s=r.get(a);if(!s||s.index<l.index||l.item.nodes.length!==1||s.item.nodes.length!==1)continue;let d={type:"move",node:l.node,sourceParent:l.item.parent,destParent:s.item.parent,source:n.get(l.item),dest:n.get(s.item),sourceSlot:l.item.primitive,destSlot:s.item.primitive,targets:[E(l.node,"move")]};i.set(l.item,{step:d,at:s.item}),i.set(s.item,{step:d,at:s.item})}return i}function It(e,n,t,r){let i=e.primitive.kind;return{type:i==="add"?"add":"restore",parent:e.parent,nodes:e.nodes,seqBefore:n.before,seqAfter:n.after,slot:e.primitive,content:e.nodes.map(a=>z(a,t,e.parent,r)),ids:i==="remove"?e.nodes.flatMap(a=>G(a)):[],targets:Et(e.nodes,i==="add"?"added":"removed",e.parent)}}function Pt(e){return{type:e.kind,primitive:e,targets:[E(e.target,e.name||e.prop||"text")]}}function Ie(e,n,t={}){if(!e||!e.replayable)return{ok:!1,code:"unsupported",targets:[]};let r={isIgnored:t.isIgnored||null,ignoreAttribute:t.ignoreAttribute||null,isProtected:t.isProtected||null,inScope:t.inScope||(()=>!0),liveNodesWithIds:t.liveNodesWithIds||(()=>[]),sim:new Map,planned:new Map,operations:[]};for(let i=0;i<e.steps.length;i++){let a=e.steps[n==="undo"?e.steps.length-1-i:i],l=Rt(a,n,r);if(l)return{ok:!1,code:l.code,targets:l.targets}}return{ok:!0,operations:r.operations}}function M(e,n){return e.sim.has(n)||e.sim.set(n,Se(n,e.isIgnored)),e.sim.get(n)}function Pe(e,n){for(let t=e;t;t=t.parentNode)if(n.planned.has(t))return n.planned.get(t);return n.inScope(e)}function Ce(e,n){return n.liveNodesWithIds(e).filter(t=>Pe(t,n)).length>0}function b(e,n){return{code:e,targets:n||[]}}function Ct(e){return e.kind==="text"?e.target.data:e.kind==="value"?typeof e.read=="function"?e.read(e.target):e.target[e.prop||"value"]:e.target.getAttribute(e.name)}function Rt(e,n,t){let r=n==="undo";return e.type==="move"?Lt(e,r,t):e.type==="add"?Ot(e,r,t):e.type==="restore"?_t(e,r,t):xt(e,r,t)}function xt(e,n,t){let r=e.primitive,i=n?r.newValue:r.oldValue,a=n?r.oldValue:r.newValue;return v(r.target)?Pe(r.target,t)?r.kind==="value"&&typeof r.write=="function"&&typeof r.read!="function"?b("unsupported",e.targets):Ct(r)!==i?b("content-changed",e.targets):(r.kind==="value"?t.operations.push({type:"value",target:r.target,prop:r.prop||"value",value:a,read:r.read||null,write:r.write||null}):r.kind==="text"?t.operations.push({type:"text",target:r.target,data:a}):t.operations.push({type:"attr",target:r.target,name:r.name,value:a??null}),null):b("target-detached",e.targets):b("unsupported",e.targets)}function Ot(e,n,t){if(!v(e.parent)||!t.inScope(e.parent))return b("target-detached",e.targets);if(n){if(!L(M(t,e.parent),e.seqAfter))return b("structure-changed",e.targets);let s=Te(e.nodes,t.isProtected);if(s.length>0)return b("protected",s);for(let d=0;d<e.nodes.length;d++)if(z(e.nodes[d],t.ignoreAttribute,e.parent,t.isIgnored)!==e.content[d])return b("content-changed",e.targets);for(let d of e.nodes)t.operations.push({type:"remove",node:d}),t.planned.set(d,!1);return t.sim.set(e.parent,e.seqBefore.slice()),null}if(!L(M(t,e.parent),e.seqBefore))return b("structure-changed",e.targets);for(let s of e.nodes)if(s.parentNode)return b("structure-changed",e.targets);let r=e.nodes.flatMap(s=>G(s));if(r.length>0&&Ce(r,t))return b("duplicate-id",e.targets);let i=D(e.seqBefore,e.slot);if(i==null)return b("unsupported",e.targets);let a=e.seqBefore.slice(),l=i;for(let s of e.nodes)t.operations.push({type:"insert",parent:e.parent,node:s,beforeId:a[l]??null}),a=a.slice(0,l).concat(T(s),a.slice(l)),l+=1,t.planned.set(s,!0);return t.sim.set(e.parent,e.seqAfter.slice()),null}function _t(e,n,t){if(!v(e.parent)||!t.inScope(e.parent))return b("target-detached",e.targets);if(n){if(!L(M(t,e.parent),e.seqAfter))return b("structure-changed",e.targets);for(let s=0;s<e.nodes.length;s++){let d=e.nodes[s];if(d.parentNode||z(d,t.ignoreAttribute,e.parent,t.isIgnored)!==e.content[s])return b("content-changed",e.targets)}if(e.ids.length>0&&Ce(e.ids,t))return b("duplicate-id",e.targets);let i=D(e.seqAfter,e.slot);if(i==null)return b("unsupported",e.targets);let a=e.seqAfter.slice(),l=i;for(let s of e.nodes)t.operations.push({type:"insert",parent:e.parent,node:s,beforeId:a[l]??null}),a=a.slice(0,l).concat(T(s),a.slice(l)),l+=1,t.planned.set(s,!0);return t.sim.set(e.parent,e.seqBefore.slice()),null}if(!L(M(t,e.parent),e.seqBefore))return b("structure-changed",e.targets);let r=Te(e.nodes,t.isProtected);if(r.length>0)return b("protected",r);for(let i=0;i<e.nodes.length;i++){let a=e.nodes[i];if(a.parentNode!==e.parent||z(a,t.ignoreAttribute,e.parent,t.isIgnored)!==e.content[i])return b("content-changed",e.targets)}for(let i of e.nodes)t.operations.push({type:"remove",node:i}),t.planned.set(i,!1);return t.sim.set(e.parent,e.seqAfter.slice()),null}function Mt(e,n){let t=e.indexOf(n);return t<0?null:e.slice(0,t).concat(e.slice(t+1))}function Lt(e,n,t){let r=T(e.node),i=n?e.destParent:e.sourceParent,a=n?e.sourceParent:e.destParent,l=M(t,i),s=M(t,a),d=n?e.dest.after:e.source.before,g=n?e.source.after:e.dest.before,p=n?e.sourceSlot:e.destSlot;if(!v(i)||!v(a)||!t.inScope(i)||!t.inScope(a))return b("target-detached",e.targets);if(!L(l,d))return b("structure-changed",e.targets);let h=i===a?Mt(s,r):s;if(!L(h,g))return b("structure-changed",e.targets);let w=D(g,p);return w==null?b("unsupported",e.targets):(t.operations.push({type:"insert",parent:a,node:e.node,beforeId:g[w]??null}),t.planned.set(e.node,!0),n?(t.sim.set(a,e.source.before.slice()),i!==a&&t.sim.set(i,e.dest.before.slice())):(t.sim.set(a,e.dest.after.slice()),i!==a&&t.sim.set(i,e.source.after.slice())),null)}function L(e,n){if(!e||!n||e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}function qt(e,n){for(let t of e.childNodes||[])if(T(t)===n)return t;return null}function Ee(e,n){typeof e.write=="function"?e.write(e.target,n):e.target[e.prop]=n}function Re(e){let n=[];try{for(let t of e)zt(t,n);return{ok:!0}}catch(t){for(let r=n.length-1;r>=0;r--)try{n[r]()}catch{}return{ok:!1,error:t}}}function zt(e,n){if(e.type==="attr"){let t=e.target.getAttribute(e.name);e.value==null?e.target.removeAttribute(e.name):e.target.setAttribute(e.name,e.value),n.push(()=>{t==null?e.target.removeAttribute(e.name):e.target.setAttribute(e.name,t)});return}if(e.type==="text"){let t=e.target.data;e.target.data=e.data,n.push(()=>{e.target.data=t});return}if(e.type==="value"){let t=typeof e.read=="function"?e.read(e.target):e.target[e.prop];Ee(e,e.value),n.push(()=>Ee(e,t));return}if(e.type==="insert"){let t=e.beforeId==null?null:qt(e.parent,e.beforeId);if(e.beforeId!=null&&!t)throw new Error("hyper-undo: replay anchor vanished");let r=e.node.parentNode,i=e.node.nextSibling;e.parent.insertBefore(e.node,t),n.push(()=>{r?r.insertBefore(e.node,i&&i.parentNode===r?i:null):e.node.parentNode===e.parent&&e.parent.removeChild(e.node)});return}if(e.type==="remove"){let t=e.node.parentNode;if(!t)throw new Error("hyper-undo: replay target is no longer attached");let r=e.node.nextSibling;t.removeChild(e.node),n.push(()=>t.insertBefore(e.node,r))}}var Kt=['[src^="chrome-extension://"]','[href^="chrome-extension://"]','[src^="moz-extension://"]','[href^="moz-extension://"]','[src^="safari-web-extension://"]','[href^="safari-web-extension://"]','[id="1p-menu-live-region"]',"com-1password-button","com-1password-menu","com-1password-notification","grammarly-extension","grammarly-desktop-integration","lt-div","lt-card","lt-toolbar","lt-highlighter","[data-lastpass-root]","[data-lastpass-icon-root]","[data-dashlane-shadowhost]","[data-klarna-extension]",'[class*="klarna-extension"]',".skiptranslate","#goog-gt-tt",".goog-te-banner-frame",".goog-te-balloon-frame","style.darkreader","style[data-darkreader-mode]","style[data-darkreader-scheme]","#loom-companion-mv3","loom-container","[ext-id]"],$t=["data-grammarly","data-lt","data-1p","data-com-onepassword","data-lastpass","data-lp","data-kwimpala","data-bitwarden","data-bw","data-dashlane","data-np","data-protonpass","data-proton-pass","data-keeper","data-rf","data-roboform","data-honey","data-rakuten","data-capital-one","data-wikibuy","data-klarna","data-darkreader","data-evernote","data-pocket","data-pin"],xe=Kt.join(","),Oe=new RegExp("^(?:"+$t.join("|")+")(?:-|$)");var rn=Object.freeze({data:Object.freeze({tokens:["no-data"],bundles:["editor-ui"]}),save:Object.freeze({tokens:["no-save"],bundles:["editor-ui"]}),snapshot:Object.freeze({tokens:["no-snapshot"],bundles:["editor-ui"]}),watch:Object.freeze({tokens:["no-watch"],bundles:["editor-ui"]}),undo:Object.freeze({tokens:["no-undo"],bundles:["editor-ui"]}),history:Object.freeze({tokens:[],bundles:["editor-ui"]})}),Bt=Object.freeze({"editor-ui":Object.freeze(["no-data","no-save","no-snapshot","no-watch","no-undo"])}),Dt=Object.freeze(["no-save","no-snapshot","no-trigger-autosave","no-dirty","no-watch","no-undo","no-data","freeze","editor-ui","live"]);function U(e,n){if(!e||e.nodeType!==1)return!1;let t=e.getAttribute?.("clay");return!!(t&&t.split(/\s+/).includes(n)||e.hasAttribute?.(n))}function Y(e,n){if(U(e,n))return!0;for(let[t,r]of Object.entries(Bt))if(r.includes(n)&&U(e,t))return!0;return!1}function _e(e){return e==="clay"||Dt.includes(e)}var Ut=["mutations-ignore","save-remove","save-ignore","save-freeze","no-undo","no-watch"];function jt(){if(typeof window>"u")return null;let e=window.clay?.region||window.hyperclay?.region;return e&&typeof e.resolveRegionPolicy=="function"?e.resolveRegionPolicy:null}function Z(e){let n=jt();if(n)try{if(!n(e).undoable)return!0}catch{}let t=e&&e.nodeType!==1?e.parentElement:e;if(t&&t.closest&&t.closest(xe))return!0;for(;t&&t.nodeType===1;){if(Y(t,"no-undo")||Y(t,"no-watch"))return!0;for(let r of Ut)if(U(t,r))return!0;t=t.parentElement}return!1}function Wt(e){let n=e.target,t=n.cloneNode(!1),r=t;for(;n.parentElement;){n=n.parentElement;let i=n.cloneNode(!1);i.appendChild(t),t=i}return e.oldValue==null?r.removeAttribute(e.attributeName):r.setAttribute(e.attributeName,e.oldValue),Z(r)}function j(e,n,t){if(t?.type==="attributes"&&_e(t.attributeName)){if(Z(e)&&Wt(t))return!0}else if(Z(e))return!0;if(t&&t.type==="attributes"){if(t.attributeName&&Oe.test(t.attributeName.toLowerCase()))return!0;if(n)try{if(n(t.attributeName,t.target))return!0}catch{}}return!1}function Me(){let e=new Map;function n(i,a){let l=e.get(i);return l||(l=new Set,e.set(i,l)),l.add(a),()=>t(i,a)}function t(i,a){let l=e.get(i);l&&l.delete(a)}function r(i,a){let l=e.get(i);if(l)for(let s of Array.from(l))try{s(a)}catch(d){console.error("[hyper-undo] listener threw",d)}}return{on:n,off:t,emit:r}}var Ft={maxHistory:100,idleWindowMs:500,idleLabel:"Edit",recording:"automatic",ignoreAttribute:null,isProtected:null,ignoreNode:null,debug:!1};function Q(e){let n={...Ft,...e};if(!n.scope)throw new Error("hyper-undo: scope is required");let t=Me(),r=[],i=[],a=`u${Math.random().toString(36).slice(2,8)}`,l=0,s=null,d=[],g=null,p=null,h=0,w=!1,P=n.recording==="commands"?"commands":"automatic",A=null,C=!1;function k(...o){n.debug&&console.log("[hyper-undo]",...o)}function $e(){w||(w=!0,p=Be(R),p.observe(n.scope,{childList:!0,attributes:!0,characterData:!0,subtree:!0,attributeOldValue:!0,characterDataOldValue:!0}),k("started"))}function Be(o){let u=typeof document<"u"&&n.scope===document.body,c=typeof window<"u"&&(window.clay?.Mutation||window.hyperclay?.Mutation)||null;return u&&c&&typeof c.createObserver=="function"?(k("sourcing records from the platform Mutation hub (shared observer)"),c.createObserver(o)):new MutationObserver(o)}function re(){w&&(w=!1,p&&(p.disconnect(),p=null),x(),r.length=0,i.length=0,d=[],s=null,A=null,C=!1,h=0,k("stopped"))}function De(){let o=n.scope;return!!(o&&o.nodeType===1&&o.isConnected===!1)}function R(o){oe(o,!1)}function oe(o,u){if(A){for(let m of o)A.records.push(m);return}if(P==="commands"||h>0)return;let c=W(Ue(o),u);if(c.length>0){for(let m of c)d.push(m);ie()}}function Ue(o){return typeof n.ignoreNode!="function"?o:o.filter(u=>{if(u.type!=="childList")return!0;let c=[...u.addedNodes,...u.removedNodes];return c.length===0?!0:!c.every(m=>je(m))})}function je(o){try{return!!n.ignoreNode(o)}catch{return!1}}function W(o,u=!1){let c=o.filter(m=>(ut(m.target)||u&&ye(m.target))&&!j(m.target,n.ignoreAttribute,m));return ve(c,m=>j(m,n.ignoreAttribute))}function ie(){x(),g=setTimeout(ae,n.idleWindowMs)}function x(){g!=null&&(clearTimeout(g),g=null)}function ae(){if(g=null,d.length===0)return;let o=d;d=[],K(n.idleLabel,o)}function K(o,u){let c=Ve(u,{isIgnored:F,ignoreAttribute:ue});if(c.steps.length===0)return k("commit",o,"with nothing to record"),t.emit("commit"),null;let m={id:`${a}-${++l}`,label:o,timestamp:Date.now(),primitives:u,certificate:c};for(r.push(m),i.length=0,s=null;r.length>n.maxHistory;)r.shift();return k("commit",o,"primitives:",u.length),t.emit("commit"),m}function F(o){return j(o,n.ignoreAttribute)}function ue(o,u,c=null){if(typeof n.ignoreAttribute!="function")return!1;try{return!!n.ignoreAttribute(o,u,c)}catch{return!1}}function We(o){if(typeof n.isProtected!="function")return!1;try{return!!n.isProtected(o)}catch{return!1}}function Fe(o,u){if(!w)throw new Error("hyper-undo: scope not started");if(P==="commands")return de(o,u,{honorPause:!0,receipt:!1});R(p.takeRecords()),O();let c=h===0?se():null,m=u();if(m&&typeof m.then=="function")throw new Error("hyper-undo: commit() fn must be synchronous; returned a Promise");if(oe(p.takeRecords(),!0),c!=null&&le(c),d.length===0){t.emit("commit");return}x();let y=d;d=[],K(o,y)}function Xe(o){if(!w)throw new Error("hyper-undo: scope not started");if(!p)return;if(h>1){p.takeRecords();return}O();let u=W(p.takeRecords(),!0);u.length!==0&&(x(),K(o,u))}function He(){p&&p.takeRecords()}function X(){let o=n.scope;return o&&o.ownerDocument||(typeof document<"u"?document:null)}function $(){let o=X();return!o||!o.head||typeof o.head.querySelector!="function"?null:o.head.querySelector("title")}function se(){let o=$();return o?o.textContent:null}function le(o){let u=$();if(!u||o==null)return;let c=u.textContent;c!==o&&fe({kind:"value",target:u,prop:"textContent",oldValue:o,newValue:c})}function Je(o){if(!o)return null;let u=X();if(!u||o.target!==u||o.prop!=="title")return o;let c=$();return c?{kind:"value",target:c,prop:"textContent",oldValue:o.oldValue,newValue:o.newValue}:null}function Ge(){if(typeof window>"u")return!1;let o=window.clay?.effect||window.hyperclay?.effect;return!!(o&&o.running)}function Ye(){p&&R(p.takeRecords()),O(),A={records:[],primitives:[]}}function Ze(){let o=A;if(A=null,!o)return[];let u=W(o.records,!0);for(let c of o.primitives)u.push(c);return u}function de(o,u,{honorPause:c,receipt:m}){if(typeof u!="function")throw new Error("hyper-undo: command() needs a synchronous function");if(A)throw new Error("hyper-undo: command() cannot nest inside another command");if(C)throw new Error("hyper-undo: command() cannot run during undo or redo");if(Ge())throw new Error("hyper-undo: command() cannot run inside a derived-effect context");if(u.constructor&&u.constructor.name==="AsyncFunction")throw new Error("hyper-undo: command() fn must be synchronous; it was declared async");let y=c&&h>0,N=y?null:se();Ye();let S=[],V=null,_;try{try{_=u()}catch(be){V=be}if(p&&R(p.takeRecords()),_&&typeof _.then=="function"){try{_.then(void 0,()=>{})}catch{}V||(V=new Error("hyper-undo: command() fn must be synchronous; returned a Promise"))}y||le(N)}finally{S=Ze()}let B=y||S.length===0?null:K(o,S);if(V)throw V;return m?B?{id:B.id,label:B.label}:null:B}function Qe(o,u){if(!w)throw new Error("hyper-undo: scope not started");return de(o,u,{honorPause:!1,receipt:!0})}function et(o){if(o!=="automatic"&&o!=="commands")throw new Error("hyper-undo: setRecording() takes 'automatic' or 'commands'");if(A)throw new Error("hyper-undo: setRecording() cannot change while a command is running");return P===o||(O(),P=o,k("recording",o)),P}function ce(o){if(!w||!o||!A&&(h>0||P==="commands"))return;let u=Je(o);u&&fe(u)}function fe(o){if(A){A.primitives.push(o);return}d.push(o),ie()}function tt(o,{prop:u="value",oldValue:c,newValue:m,read:y,write:N}={}){!o||c===m||ce({kind:"value",target:o,prop:u,oldValue:c,newValue:m,read:y,write:N})}function O(){h===0&&p&&R(p.takeRecords()),d.length!==0&&(x(),ae())}function me(){h===0&&p&&R(p.takeRecords()),h++,k("paused",h)}function pe(){h!==0&&(h--,h===0&&p&&p.takeRecords(),k("resumed",h))}function ge(o,u){if(De())return re(),{ok:!1,code:"disconnected"};O();let c=o==="undo"?r:i,m=o==="undo"?i:r,y=c.at(-1);if(!y)return{ok:!1,code:"empty"};if(u&&u!==y.id){let _=he(u)||y;return H(_,o,"stale-entry")}let N;C=!0;try{N=Ie(y.certificate,o,{isIgnored:F,ignoreAttribute:ue,isProtected:We,inScope:st,liveNodesWithIds:lt})}finally{C=!1}if(!N.ok)return H(y,o,N.code,N.targets);me();let S;C=!0;try{S=Re(N.operations)}finally{C=!1,pe()}if(!S.ok)return k(`${o} failed and was rolled back`,S.error),H(y,o,"replay-error");c.pop(),m.push(y),s=null;let V={ok:!0,entryId:y.id,label:y.label,direction:o};return k(o,y.label),t.emit(o,V),V}function nt(o){return ge("undo",o&&o.entryId)}function rt(o){return ge("redo",o&&o.entryId)}function he(o){return r.find(u=>u.id===o)||i.find(u=>u.id===o)||null}function H(o,u,c,m=[]){return s={entryId:o.id,direction:u,code:c,targets:m},k("refused",u,c),t.emit("refused",{entryId:o.id,label:o.label,direction:u,code:c,targets:m}),{ok:!1,code:c,entryId:o.id,label:o.label,direction:u}}function ot(o){if(!s||s.entryId!==o)return{ok:!1,code:"not-refused",entryId:o};let u=s.direction,c=u==="undo"?r:i,m=c.at(-1);return!m||m.id!==o?{ok:!1,code:"stale-entry",entryId:o}:(c.pop(),s=null,k("discarded",m.label),t.emit("discard",{entryId:o,label:m.label,direction:u}),{ok:!0,entryId:o,direction:u})}function it(o){let u=he(o);if(!u||!u.certificate||!u.certificate.replayable)return null;let c=s&&s.entryId===u.id?{direction:s.direction,code:s.code,targets:(s.targets||[]).map(m=>({id:m.id??null,kind:m.kind??null}))}:null;return{id:u.id,label:u.label,timestamp:u.timestamp,refused:c,steps:u.certificate.steps.map(at)}}function at(o){if(o.type==="move")return{kind:"move",node:{id:E(o.node).id},from:{id:E(o.sourceParent).id},to:{id:E(o.destParent).id}};if(o.type==="add"||o.type==="restore")return{kind:o.type==="add"?"add":"remove",parent:{id:E(o.parent).id},nodes:o.nodes.map((N,S)=>({id:E(N).id,html:o.content[S]??null}))};let u=o.primitive,c={id:E(u.target).id},m=u.oldValue??null,y=u.newValue??null;return o.type==="value"?{kind:"value",target:c,prop:u.prop??"value",before:m,after:y}:o.type==="text"?{kind:"text",target:c,before:m,after:y}:o.type==="attr-set"||o.type==="attr-add"||o.type==="attr-remove"?{kind:"attribute",target:c,name:u.name??null,before:m,after:y}:{kind:o.type,target:c,before:m,after:y}}function ut(o){let u=n.scope;return!u||!o?!1:o===u||typeof u.contains=="function"&&u.contains(o)}function ye(o){let u=X(),c=u&&u.head;if(!c||!o)return!1;let m=o.nodeType===1?o:o.parentElement;for(;m&&m!==c;m=m.parentElement)if(m.localName==="title")return!1;return m===c}function st(o){let u=n.scope;return!o||!u?!1:o===u||$()===o||ye(o)?!0:typeof u.contains=="function"?u.contains(o):!1}function lt(o){let u=n.scope;if(!o||o.length===0||typeof u?.querySelectorAll!="function")return[];let c=new Set(o),m=[];for(let y of u.querySelectorAll("[data-id]"))c.has(y.getAttribute("data-id"))&&(F(y)||m.push(y));return m}function dt(){p&&p.takeRecords(),r.length=0,i.length=0,d=[],s=null,x(),k("cleared"),t.emit("clear")}return{_config:n,start:$e,stop:re,commit:Fe,commitCaptured:Xe,discardCaptured:He,flush:O,command:Qe,setRecording:et,record:ce,recordValue:tt,undo:nt,redo:rt,clear:dt,discard:ot,describe:it,pause:me,resume:pe,on:t.on,off:t.off,get canUndo(){return r.length>0},get canRedo(){return i.length>0},get isPaused(){return h>0},get history(){return r.map(o=>{let u={id:o.id,label:o.label,timestamp:o.timestamp};return s&&s.entryId===o.id&&(u.refused={direction:s.direction,code:s.code,targets:s.targets}),u})},get future(){return i.map(o=>{let u={id:o.id,label:o.label,timestamp:o.timestamp};return s&&s.entryId===o.id&&(u.refused={direction:s.direction,code:s.code,targets:s.targets}),u}).reverse()},get scope(){return n.scope}}}var Xt=new Set(["checkbox","radio","button","submit","reset","image","file","range","color"]);function Ht(e){let n=typeof e.composedPath=="function"?e.composedPath():[],t=n.length>0?n:Jt(e.target);for(let r of t){if(!r||r.nodeType!==1)continue;let i=String(r.tagName||"").toUpperCase();if(i==="TEXTAREA"||i==="SELECT"||i==="INPUT"&&!Xt.has(String(r.type||r.getAttribute?.("type")||"text").toLowerCase())||r.isContentEditable===!0)return!0;let a=r.getAttribute&&r.getAttribute("contenteditable");if(a!=null&&String(a).toLowerCase()!=="false")return!0}return!1}function Jt(e){let n=[];for(let t=e;t;t=t.parentNode||(t.host??null))n.push(t);return n}function Le(e,n){let t=r=>{let i=e.scope;if(i&&i.nodeType===1&&i.isConnected===!1){n&&n();return}if(r.defaultPrevented||r.isComposing||r.keyCode===229||Ht(r))return;let a=r.target,l=e._config.shadowKeydownIn;if(a&&a.closest&&Array.isArray(l))for(let g of l)try{if(a.closest(g))return}catch{}if(!(r.metaKey||r.ctrlKey))return;let d=(r.key||"").toLowerCase();if(d==="z"&&!r.shiftKey){r.preventDefault(),r.stopPropagation(),e.undo();return}if(d==="z"&&r.shiftKey){r.preventDefault(),r.stopPropagation(),e.redo();return}if(d==="y"&&!r.shiftKey){r.preventDefault(),r.stopPropagation(),e.redo();return}};return window.addEventListener("keydown",t,!0),()=>window.removeEventListener("keydown",t,!0)}var ee=[".CodeMirror",".cm-editor",".monaco-editor",".ace_editor",".ql-editor",".tiptap",".ProseMirror",".overtype-input","[data-richclay-active]"],f=null,te=null,qe=null,I=null;function ze(e,n){if(I){if(I.inst===e)return I.cleanup;throw new Error("hyper-undo: another scope already owns the global Cmd+Z binding (bindKeys: true). Stop it first, or use bindKeys: false for additional scopes.")}let t=Le(e,n);return I={inst:e,cleanup:t},t}function Ke(e){I&&I.inst===e&&(I.cleanup(),I=null)}var ne={defaults:{shadowKeydownIn:ee},start(e={}){let n=e.scope||(typeof document<"u"?document.body:null);if(!n)throw new Error("hyper-undo: no scope (need document.body or explicit { scope })");if(f){if(te===n)return console.warn("[hyper-undo] start() called again on existing singleton; ignoring new options"),f;throw new Error("hyper-undo: start() called with a different scope while the singleton is already started. Use undo.create({ scope }) for additional scopes, or call undo.stop() first.")}let t={...e,scope:n,shadowKeydownIn:e.shadowKeydownIn||ee};return f=Q(t),f.start(),te=n,e.bindKeys!==!1&&(qe=ze(f,()=>ne.stop())),f},stop(){f&&(Ke(f),qe=null,f.stop(),f=null,te=null)},create(e={}){let n=e.scope||(typeof document<"u"?document.body:null);if(!n)throw new Error("hyper-undo: no scope (need document.body or explicit { scope })");let t={...e,scope:n,shadowKeydownIn:e.shadowKeydownIn||ee},r=Q(t),i=null,a={_config:r._config,commit:r.commit,command:r.command,setRecording:r.setRecording,commitCaptured:r.commitCaptured,discardCaptured:r.discardCaptured,flush:r.flush,record:r.record,recordValue:r.recordValue,undo:r.undo,redo:r.redo,clear:r.clear,discard:r.discard,describe:r.describe,pause:r.pause,resume:r.resume,on:r.on,off:r.off,get canUndo(){return r.canUndo},get canRedo(){return r.canRedo},get isPaused(){return r.isPaused},get history(){return r.history},get future(){return r.future},get scope(){return r.scope},start(){e.bindKeys&&(i=ze(r,()=>a.stop())),r.start()},stop(){Ke(r),i=null,r.stop()}};return a},commit(e,n){return f?f.commit(e,n):void 0},command(e,n){return f?f.command(e,n):void 0},setRecording(e){return f?f.setRecording(e):void 0},commitCaptured(e){return f?f.commitCaptured(e):void 0},discardCaptured(){return f?f.discardCaptured():void 0},record(e){return f?f.record(e):void 0},recordValue(e,n){return f?f.recordValue(e,n):void 0},undo(e){return f?f.undo(e):void 0},redo(e){return f?f.redo(e):void 0},clear(){return f?f.clear():void 0},discard(e){return f?f.discard(e):void 0},describe(e){return f?f.describe(e):void 0},pause(){return f?f.pause():void 0},resume(){return f?f.resume():void 0},flush(){return f?f.flush():void 0},on(e,n){return f?f.on(e,n):void 0},off(e,n){return f?f.off(e,n):void 0},get canUndo(){return f?f.canUndo:!1},get canRedo(){return f?f.canRedo:!1},get history(){return f?f.history:[]},get future(){return f?f.future:[]},get isPaused(){return f?f.isPaused:!1}};var Gt=ne;return ht(Yt);})();
|
|
1
|
+
var hyperundo=(()=>{var Y=Object.defineProperty;var mt=Object.getOwnPropertyDescriptor;var pt=Object.getOwnPropertyNames;var gt=Object.prototype.hasOwnProperty;var ht=(e,n)=>{for(var t in n)Y(e,t,{get:n[t],enumerable:!0})},yt=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of pt(n))!gt.call(e,i)&&i!==t&&Y(e,i,{get:()=>n[i],enumerable:!(r=mt(n,i))||r.enumerable});return e};var bt=e=>yt(Y({},"__esModule",{value:!0}),e);var Qt={};ht(Qt,{default:()=>Zt,undo:()=>oe});function wt(e,n=null){let t=[];if(e.type==="attributes"){let r=e.target,i=e.attributeName,a=e.oldValue,s=r.getAttribute(i);return a==null&&s!=null?t.push({kind:"attr-add",target:r,name:i,newValue:s}):a!=null&&s==null?t.push({kind:"attr-remove",target:r,name:i,oldValue:a}):a!==s&&t.push({kind:"attr-set",target:r,name:i,oldValue:a,newValue:s}),t}if(e.type==="characterData")return t.push({kind:"text",target:e.target,oldValue:e.oldValue,newValue:e.target.data}),t;if(e.type==="childList"){let r=e.target,i=e.nextSibling,a=e.nextSibling;for(;a&&n?.(a);)a=a.nextSibling;let s=e.previousSibling;for(;s&&n?.(s);)s=s.previousSibling;let l=n?Array.from(e.addedNodes).filter(h=>!n(h)):Array.from(e.addedNodes),c=n?Array.from(e.removedNodes).filter(h=>!n(h)):Array.from(e.removedNodes);return l.length>0&&t.push({kind:"add",parent:r,nodes:l,before:i,afterAuthored:a,beforeAuthored:s,record:e}),c.length>0&&t.push({kind:"remove",parent:r,nodes:c,before:i,afterAuthored:a,beforeAuthored:s,record:e}),t}return t}function Ee(e,n=null){let t=[],r=new Map;for(let i of e){if(i.type==="attributes"){let a=i.target,s=i.attributeName,l=r.get(a);l||(l=new Map,r.set(a,l)),l.has(s)||(l.set(s,{idx:t.length,oldValue:i.oldValue}),t.push(null));continue}for(let a of wt(i,n))t.push(a)}for(let[i,a]of r)for(let[s,l]of a){let c=l.oldValue,h=i.getAttribute(s),p=null;c===h?p=null:c==null?p={kind:"attr-add",target:i,name:s,newValue:h}:h==null?p={kind:"attr-remove",target:i,name:s,oldValue:c}:p={kind:"attr-set",target:i,name:s,oldValue:c,newValue:h},t[l.idx]=p}return t.filter(i=>i!=null)}function Se(e){let n=[],t=new Map;for(let i of e){let a=Ae(i);if(a==null){n.push(i);continue}let s=t.get(i.target);s||(s=new Map,t.set(i.target,s));let l=s.get(a);if(l==null){let c=Ne(i);if(c.oldValue===c.newValue)continue;s.set(a,n.length),n.push({...i,...c})}else n[l]={...n[l],newValue:Ne(i).newValue}}let r=[];for(let i of n){if(Ae(i)==null){r.push(i);continue}i.oldValue!==i.newValue&&r.push(vt(i))}return r}var kt=new Set(["attr-set","attr-add","attr-remove","text","value"]);function Ae(e){return kt.has(e.kind)?e.kind==="text"?"#text":e.kind==="value"?`#${e.prop||"value"}`:e.name==null?null:`@${e.name}`:null}function Ne(e){return e.kind==="attr-add"?{oldValue:null,newValue:e.newValue}:e.kind==="attr-remove"?{oldValue:e.oldValue,newValue:null}:{oldValue:e.oldValue,newValue:e.newValue}}function vt(e){return e.kind!=="attr-add"&&e.kind!=="attr-remove"?e:e.oldValue==null&&e.newValue!=null?{...e,kind:"attr-add"}:e.oldValue!=null&&e.newValue==null?{...e,kind:"attr-remove"}:{...e,kind:"attr-set"}}var Te=new WeakMap,At=0;function T(e){let n=Te.get(e);return n||(n=`n${++At}`,Te.set(e,n)),n}function v(e){return!!e&&typeof e=="object"&&typeof e.nodeType=="number"}function Nt(e){return e.kind==="add"||e.kind==="remove"}function Ie(e,n){let t=[],r=e.childNodes;if(!r)return t;for(let i of r)n&&n(i)||t.push(T(i));return t}function K(e,n,t=null,r=null){if(!v(e))return null;if(e.nodeType===3)return`#text:${JSON.stringify(e.data)}`;if(e.nodeType===8)return`#comment:${JSON.stringify(e.data)}`;if(e.nodeType!==1)return`#node:${e.nodeType}`;let i=[];if(e.attributes){for(let l of e.attributes)Et(l.name,e,n,t)||i.push(`${l.name}=${JSON.stringify(l.value)}`);i.sort()}let a=[];if(e.childNodes)for(let l of e.childNodes)St(l,r)||a.push(K(l,n,t,r));return`<${String(e.tagName||"").toLowerCase()}${i.length?` ${i.join(" ")}`:""}>${a.join("")}</>`}function Z(e,n=[]){if(!v(e))return n;if(e.nodeType===1){let t=e.getAttribute&&e.getAttribute("data-id");t&&n.push(t)}if(e.childNodes)for(let t of e.childNodes)Z(t,n);return n}function Et(e,n,t,r=null){if(typeof t!="function")return!1;try{return!!t(e,n,r)}catch{return!1}}function St(e,n){if(typeof n!="function")return!1;try{return!!n(e)}catch{return!1}}function Pe(e,n){if(typeof n!="function")return[];let t=[],r=i=>{if(!v(i))return;let a=!1;try{a=!!n(i)}catch{a=!1}if(a&&t.push(E(i,"protected")),i.childNodes)for(let s of i.childNodes)r(s)};for(let i of e)r(i);return t}function E(e,n,t=null){let r=e;r&&r.nodeType!==1&&(r=r.parentElement);let i=r&&typeof r.closest=="function"?r.closest("[data-id]"):null;return!i&&t&&typeof t.closest=="function"&&(i=t.closest("[data-id]")),{id:(i?i.getAttribute("data-id"):null)||null,kind:n||null}}function Tt(e,n,t=null){return(e||[]).filter(v).map(r=>E(r,n,t))}function z(e){return{replayable:!1,reason:e,steps:[]}}function U(e,n){let t=[[n.afterAuthored,i=>i],[n.beforeAuthored,i=>i+1],[n.before,i=>i]],r=!1;for(let[i,a]of t){if(!v(i))continue;r=!0;let s=e.indexOf(T(i));if(s>=0)return a(s)}return r?null:e.length}function Ce(e,n={}){let t=n.isIgnored||null,r=n.ignoreAttribute||null,i=Se(e),a=[];for(let p of i){if(!Nt(p)){if(!v(p.target))return z("non-dom-target");continue}if(!v(p.parent))return z("non-dom-parent");let y=Array.from(p.nodes||[]).filter(v);if(y.length!==0){if(y.length!==Array.from(p.nodes||[]).length)return z("non-dom-node");a.push({primitive:p,nodes:y,parent:p.parent})}}if(Vt(a))return z("in-record-move");let s=It(a,t);if(!s)return z("ambiguous-structure");let l=Pt(a,s),c=new Map(a.map(p=>[p.primitive,p])),h=[];for(let p of i){let y=c.get(p);if(!y){h.push(Rt(p));continue}let w=l.get(y);if(w){w.at===y&&h.push(w.step);continue}h.push(Ct(y,s.get(y),r,t))}return{replayable:!0,reason:null,steps:h}}function Vt(e){let n=new Map;for(let t of e){let r=t.primitive.record;if(!r)continue;let i=n.get(r);i||(i=new Map,n.set(r,i));for(let a of t.nodes){let s=i.get(a);s||(s=new Set,i.set(a,s)),s.add(t.primitive.kind)}}for(let t of n.values())for(let r of t.values())if(r.size>1)return!0;return!1}function It(e,n){let t=new Map;for(let i of e)t.has(i.parent)||t.set(i.parent,Ie(i.parent,n));let r=new Map;for(let i=e.length-1;i>=0;i--){let a=e[i],s=t.get(a.parent);if(!s)return null;let l=s.slice(),c;if(a.primitive.kind==="add"){let h=a.nodes.map(T);if(!h.every(p=>s.includes(p)))return null;c=s.filter(p=>!h.includes(p))}else{let h=U(l,a.primitive);if(h==null)return null;c=l.slice(),c.splice(h,0,...a.nodes.map(T))}r.set(a,{before:c,after:l}),t.set(a.parent,c)}return r}function Pt(e,n){let t=new Map,r=new Map;e.forEach((a,s)=>{for(let l of a.nodes){let c=T(l);a.primitive.kind==="remove"?t.set(c,{item:a,index:s,node:l}):r.set(c,{item:a,index:s,node:l})}});let i=new Map;for(let[a,s]of t){let l=r.get(a);if(!l||l.index<s.index||s.item.nodes.length!==1||l.item.nodes.length!==1)continue;let c={type:"move",node:s.node,sourceParent:s.item.parent,destParent:l.item.parent,source:n.get(s.item),dest:n.get(l.item),sourceSlot:s.item.primitive,destSlot:l.item.primitive,targets:[E(s.node,"move")]};i.set(s.item,{step:c,at:l.item}),i.set(l.item,{step:c,at:l.item})}return i}function Ct(e,n,t,r){let i=e.primitive.kind;return{type:i==="add"?"add":"restore",parent:e.parent,nodes:e.nodes,seqBefore:n.before,seqAfter:n.after,slot:e.primitive,content:e.nodes.map(a=>K(a,t,e.parent,r)),ids:i==="remove"?e.nodes.flatMap(a=>Z(a)):[],targets:Tt(e.nodes,i==="add"?"added":"removed",e.parent)}}function Rt(e){return{type:e.kind,primitive:e,targets:[E(e.target,e.name||e.prop||"text")]}}function Re(e,n,t={}){if(!e||!e.replayable)return{ok:!1,code:"unsupported",targets:[]};let r={isIgnored:t.isIgnored||null,ignoreAttribute:t.ignoreAttribute||null,isProtected:t.isProtected||null,inScope:t.inScope||(()=>!0),liveNodesWithIds:t.liveNodesWithIds||(()=>[]),sim:new Map,planned:new Map,operations:[]};for(let i=0;i<e.steps.length;i++){let a=e.steps[n==="undo"?e.steps.length-1-i:i],s=xt(a,n,r);if(s)return{ok:!1,code:s.code,targets:s.targets}}return{ok:!0,operations:r.operations}}function M(e,n){return e.sim.has(n)||e.sim.set(n,Ie(n,e.isIgnored)),e.sim.get(n)}function L(e,n){for(let t=e;t;t=t.parentNode)if(n.planned.has(t))return n.planned.get(t);return n.inScope(e)}function Oe(e,n){return n.liveNodesWithIds(e).filter(t=>L(t,n)).length>0}function b(e,n){return{code:e,targets:n||[]}}function Ot(e){return e.kind==="text"?e.target.data:e.kind==="value"?typeof e.read=="function"?e.read(e.target):e.target[e.prop||"value"]:e.target.getAttribute(e.name)}function xt(e,n,t){let r=n==="undo";return e.type==="move"?zt(e,r,t):e.type==="add"?Mt(e,r,t):e.type==="restore"?Lt(e,r,t):_t(e,r,t)}function _t(e,n,t){let r=e.primitive,i=n?r.newValue:r.oldValue,a=n?r.oldValue:r.newValue;return v(r.target)?L(r.target,t)?r.kind==="value"&&typeof r.write=="function"&&typeof r.read!="function"?b("unsupported",e.targets):Ot(r)!==i?b("content-changed",e.targets):(r.kind==="value"?t.operations.push({type:"value",target:r.target,prop:r.prop||"value",value:a,read:r.read||null,write:r.write||null}):r.kind==="text"?t.operations.push({type:"text",target:r.target,data:a}):t.operations.push({type:"attr",target:r.target,name:r.name,value:a??null}),null):b("target-detached",e.targets):b("unsupported",e.targets)}function Mt(e,n,t){if(!v(e.parent)||!L(e.parent,t))return b("target-detached",e.targets);if(n){if(!q(M(t,e.parent),e.seqAfter))return b("structure-changed",e.targets);let l=Pe(e.nodes,t.isProtected);if(l.length>0)return b("protected",l);for(let c=0;c<e.nodes.length;c++)if(K(e.nodes[c],t.ignoreAttribute,e.parent,t.isIgnored)!==e.content[c])return b("content-changed",e.targets);for(let c of e.nodes)t.operations.push({type:"remove",node:c}),t.planned.set(c,!1);return t.sim.set(e.parent,e.seqBefore.slice()),null}if(!q(M(t,e.parent),e.seqBefore))return b("structure-changed",e.targets);for(let l of e.nodes)if(l.parentNode)return b("structure-changed",e.targets);let r=e.nodes.flatMap(l=>Z(l));if(r.length>0&&Oe(r,t))return b("duplicate-id",e.targets);let i=U(e.seqBefore,e.slot);if(i==null)return b("unsupported",e.targets);let a=e.seqBefore.slice(),s=i;for(let l of e.nodes)t.operations.push({type:"insert",parent:e.parent,node:l,beforeId:a[s]??null}),a=a.slice(0,s).concat(T(l),a.slice(s)),s+=1,t.planned.set(l,!0);return t.sim.set(e.parent,e.seqAfter.slice()),null}function Lt(e,n,t){if(!v(e.parent)||!L(e.parent,t))return b("target-detached",e.targets);if(n){if(!q(M(t,e.parent),e.seqAfter))return b("structure-changed",e.targets);for(let c=0;c<e.nodes.length;c++){let h=e.nodes[c];if(h.parentNode||K(h,t.ignoreAttribute,e.parent,t.isIgnored)!==e.content[c])return b("content-changed",e.targets)}if(e.ids.length>0&&Oe(e.ids,t))return b("duplicate-id",e.targets);let a=U(e.seqAfter,e.slot);if(a==null)return b("unsupported",e.targets);let s=e.seqAfter.slice(),l=a;for(let c of e.nodes)t.operations.push({type:"insert",parent:e.parent,node:c,beforeId:s[l]??null}),s=s.slice(0,l).concat(T(c),s.slice(l)),l+=1,t.planned.set(c,!0);return t.sim.set(e.parent,e.seqBefore.slice()),null}if(!q(M(t,e.parent),e.seqBefore))return b("structure-changed",e.targets);let r=Pe(e.nodes,t.isProtected);if(r.length>0)return b("protected",r);let i=a=>t.planned.get(a)===!0||typeof t.isIgnored=="function"&&t.isIgnored(a);for(let a=0;a<e.nodes.length;a++){let s=e.nodes[a];if(s.parentNode!==e.parent||K(s,t.ignoreAttribute,e.parent,i)!==e.content[a])return b("content-changed",e.targets)}for(let a of e.nodes)t.operations.push({type:"remove",node:a}),t.planned.set(a,!1);return t.sim.set(e.parent,e.seqAfter.slice()),null}function qt(e,n){let t=e.indexOf(n);return t<0?null:e.slice(0,t).concat(e.slice(t+1))}function zt(e,n,t){let r=T(e.node),i=n?e.destParent:e.sourceParent,a=n?e.sourceParent:e.destParent,s=M(t,i),l=M(t,a),c=n?e.dest.after:e.source.before,h=n?e.source.after:e.dest.before,p=n?e.sourceSlot:e.destSlot;if(!v(i)||!v(a)||!L(i,t)||!L(a,t))return b("target-detached",e.targets);if(!q(s,c))return b("structure-changed",e.targets);let y=i===a?qt(l,r):l;if(!q(y,h))return b("structure-changed",e.targets);let w=U(h,p);return w==null?b("unsupported",e.targets):(t.operations.push({type:"insert",parent:a,node:e.node,beforeId:h[w]??null}),t.planned.set(e.node,!0),n?(t.sim.set(a,e.source.before.slice()),i!==a&&t.sim.set(i,e.dest.before.slice())):(t.sim.set(a,e.dest.after.slice()),i!==a&&t.sim.set(i,e.source.after.slice())),null)}function q(e,n){if(!e||!n||e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}function Kt(e,n){for(let t of e.childNodes||[])if(T(t)===n)return t;return null}function Ve(e,n){typeof e.write=="function"?e.write(e.target,n):e.target[e.prop]=n}function xe(e){let n=[];try{for(let t of e)$t(t,n);return{ok:!0}}catch(t){for(let r=n.length-1;r>=0;r--)try{n[r]()}catch{}return{ok:!1,error:t}}}function $t(e,n){if(e.type==="attr"){let t=e.target.getAttribute(e.name);e.value==null?e.target.removeAttribute(e.name):e.target.setAttribute(e.name,e.value),n.push(()=>{t==null?e.target.removeAttribute(e.name):e.target.setAttribute(e.name,t)});return}if(e.type==="text"){let t=e.target.data;e.target.data=e.data,n.push(()=>{e.target.data=t});return}if(e.type==="value"){let t=typeof e.read=="function"?e.read(e.target):e.target[e.prop];Ve(e,e.value),n.push(()=>Ve(e,t));return}if(e.type==="insert"){let t=e.beforeId==null?null:Kt(e.parent,e.beforeId);if(e.beforeId!=null&&!t)throw new Error("hyper-undo: replay anchor vanished");let r=e.node.parentNode,i=e.node.nextSibling;e.parent.insertBefore(e.node,t),n.push(()=>{r?r.insertBefore(e.node,i&&i.parentNode===r?i:null):e.node.parentNode===e.parent&&e.parent.removeChild(e.node)});return}if(e.type==="remove"){let t=e.node.parentNode;if(!t)throw new Error("hyper-undo: replay target is no longer attached");let r=e.node.nextSibling;t.removeChild(e.node),n.push(()=>t.insertBefore(e.node,r))}}var Bt=['[src^="chrome-extension://"]','[href^="chrome-extension://"]','[src^="moz-extension://"]','[href^="moz-extension://"]','[src^="safari-web-extension://"]','[href^="safari-web-extension://"]','[id="1p-menu-live-region"]',"com-1password-button","com-1password-menu","com-1password-notification","grammarly-extension","grammarly-desktop-integration","lt-div","lt-card","lt-toolbar","lt-highlighter","[data-lastpass-root]","[data-lastpass-icon-root]","[data-dashlane-shadowhost]","[data-klarna-extension]",'[class*="klarna-extension"]',".skiptranslate","#goog-gt-tt",".goog-te-banner-frame",".goog-te-balloon-frame","style.darkreader","style[data-darkreader-mode]","style[data-darkreader-scheme]","#loom-companion-mv3","loom-container","[ext-id]"],Dt=["data-grammarly","data-lt","data-1p","data-com-onepassword","data-lastpass","data-lp","data-kwimpala","data-bitwarden","data-bw","data-dashlane","data-np","data-protonpass","data-proton-pass","data-keeper","data-rf","data-roboform","data-honey","data-rakuten","data-capital-one","data-wikibuy","data-klarna","data-darkreader","data-evernote","data-pocket","data-pin"],_e=Bt.join(","),Me=new RegExp("^(?:"+Dt.join("|")+")(?:-|$)");var an=Object.freeze({data:Object.freeze({tokens:["no-data"],bundles:["editor-ui"]}),save:Object.freeze({tokens:["no-save"],bundles:["editor-ui"]}),snapshot:Object.freeze({tokens:["no-snapshot"],bundles:["editor-ui"]}),watch:Object.freeze({tokens:["no-watch"],bundles:["editor-ui"]}),undo:Object.freeze({tokens:["no-undo"],bundles:["editor-ui"]}),history:Object.freeze({tokens:[],bundles:["editor-ui"]})}),Ut=Object.freeze({"editor-ui":Object.freeze(["no-data","no-save","no-snapshot","no-watch","no-undo"])}),jt=Object.freeze(["no-save","no-snapshot","no-trigger-autosave","no-dirty","no-watch","no-undo","no-data","freeze","editor-ui","live"]);function j(e,n){if(!e||e.nodeType!==1)return!1;let t=e.getAttribute?.("clay");return!!(t&&t.split(/\s+/).includes(n)||e.hasAttribute?.(n))}function Q(e,n){if(j(e,n))return!0;for(let[t,r]of Object.entries(Ut))if(r.includes(n)&&j(e,t))return!0;return!1}function Le(e){return e==="clay"||jt.includes(e)}var Ft=["mutations-ignore","save-remove","save-ignore","save-freeze","no-undo","no-watch"];function Wt(){if(typeof window>"u")return null;let e=window.clay?.region||window.hyperclay?.region;return e&&typeof e.resolveRegionPolicy=="function"?e.resolveRegionPolicy:null}function ee(e){let n=Wt();if(n)try{if(!n(e).undoable)return!0}catch{}let t=e&&e.nodeType!==1?e.parentElement:e;if(t&&t.closest&&t.closest(_e))return!0;for(;t&&t.nodeType===1;){if(Q(t,"no-undo")||Q(t,"no-watch"))return!0;for(let r of Ft)if(j(t,r))return!0;t=t.parentElement}return!1}function Xt(e){let n=e.target,t=n.cloneNode(!1),r=t;for(;n.parentElement;){n=n.parentElement;let i=n.cloneNode(!1);i.appendChild(t),t=i}return e.oldValue==null?r.removeAttribute(e.attributeName):r.setAttribute(e.attributeName,e.oldValue),ee(r)}function F(e,n,t){if(t?.type==="attributes"&&Le(t.attributeName)){if(ee(e)&&Xt(t))return!0}else if(ee(e))return!0;if(t&&t.type==="attributes"){if(t.attributeName&&Me.test(t.attributeName.toLowerCase()))return!0;if(n)try{if(n(t.attributeName,t.target))return!0}catch{}}return!1}function qe(){let e=new Map;function n(i,a){let s=e.get(i);return s||(s=new Set,e.set(i,s)),s.add(a),()=>t(i,a)}function t(i,a){let s=e.get(i);s&&s.delete(a)}function r(i,a){let s=e.get(i);if(s)for(let l of Array.from(s))try{l(a)}catch(c){console.error("[hyper-undo] listener threw",c)}}return{on:n,off:t,emit:r}}var Ht={maxHistory:100,idleWindowMs:500,idleLabel:"Edit",recording:"automatic",ignoreAttribute:null,isProtected:null,ignoreNode:null,debug:!1};function te(e){let n={...Ht,...e};if(!n.scope)throw new Error("hyper-undo: scope is required");let t=qe(),r=[],i=[],a=`u${Math.random().toString(36).slice(2,8)}`,s=0,l=null,c=[],h=null,p=null,y=0,w=!1,P=n.recording==="commands"?"commands":"automatic",A=null,C=!1;function k(...o){n.debug&&console.log("[hyper-undo]",...o)}function De(){w||(w=!0,p=Ue(R),p.observe(n.scope,{childList:!0,attributes:!0,characterData:!0,subtree:!0,attributeOldValue:!0,characterDataOldValue:!0}),k("started"))}function Ue(o){let u=typeof document<"u"&&n.scope===document.body,d=typeof window<"u"&&(window.clay?.Mutation||window.hyperclay?.Mutation)||null;return u&&d&&typeof d.createObserver=="function"?(k("sourcing records from the platform Mutation hub (shared observer)"),d.createObserver(o)):new MutationObserver(o)}function ie(){w&&(w=!1,p&&(p.disconnect(),p=null),O(),r.length=0,i.length=0,c=[],l=null,A=null,C=!1,y=0,k("stopped"))}function je(){let o=n.scope;return!!(o&&o.nodeType===1&&o.isConnected===!1)}function R(o){ae(o,!1)}function ae(o,u){if(A){for(let f of o)A.records.push(f);return}if(P==="commands"||y>0)return;let d=W(Fe(o),u);if(d.length>0){for(let f of d)c.push(f);ue()}}function Fe(o){return typeof n.ignoreNode!="function"?o:o.filter(u=>{if(u.type!=="childList")return!0;let d=[...u.addedNodes,...u.removedNodes];return d.length===0?!0:!d.every(f=>We(f))})}function We(o){try{return!!n.ignoreNode(o)}catch{return!1}}function W(o,u=!1){let d=lt(o),f=o.filter(g=>(G(g.target)||d(g.target)||u&&ke(g.target))&&!F(g.target,n.ignoreAttribute,g));return Ee(f,g=>F(g,n.ignoreAttribute))}function ue(){O(),h=setTimeout(se,n.idleWindowMs)}function O(){h!=null&&(clearTimeout(h),h=null)}function se(){if(h=null,c.length===0)return;let o=c;c=[],$(n.idleLabel,o)}function $(o,u){let d=Ce(u,{isIgnored:X,ignoreAttribute:le});if(d.steps.length===0)return k("commit",o,"with nothing to record"),t.emit("commit"),null;let f={id:`${a}-${++s}`,label:o,timestamp:Date.now(),primitives:u,certificate:d};for(r.push(f),i.length=0,l=null;r.length>n.maxHistory;)r.shift();return k("commit",o,"primitives:",u.length),t.emit("commit"),f}function X(o){return F(o,n.ignoreAttribute)}function le(o,u,d=null){if(typeof n.ignoreAttribute!="function")return!1;try{return!!n.ignoreAttribute(o,u,d)}catch{return!1}}function Xe(o){if(typeof n.isProtected!="function")return!1;try{return!!n.isProtected(o)}catch{return!1}}function He(o,u){if(!w)throw new Error("hyper-undo: scope not started");if(P==="commands")return fe(o,u,{honorPause:!0,receipt:!1});R(p.takeRecords()),x();let d=y===0?de():null,f=u();if(f&&typeof f.then=="function")throw new Error("hyper-undo: commit() fn must be synchronous; returned a Promise");if(ae(p.takeRecords(),!0),d!=null&&ce(d),c.length===0){t.emit("commit");return}O();let g=c;c=[],$(o,g)}function Je(o){if(!w)throw new Error("hyper-undo: scope not started");if(!p)return;if(y>1){p.takeRecords();return}x();let u=W(p.takeRecords(),!0);u.length!==0&&(O(),$(o,u))}function Ge(){p&&p.takeRecords()}function H(){let o=n.scope;return o&&o.ownerDocument||(typeof document<"u"?document:null)}function B(){let o=H();return!o||!o.head||typeof o.head.querySelector!="function"?null:o.head.querySelector("title")}function de(){let o=B();return o?o.textContent:null}function ce(o){let u=B();if(!u||o==null)return;let d=u.textContent;d!==o&&pe({kind:"value",target:u,prop:"textContent",oldValue:o,newValue:d})}function Ye(o){if(!o)return null;let u=H();if(!u||o.target!==u||o.prop!=="title")return o;let d=B();return d?{kind:"value",target:d,prop:"textContent",oldValue:o.oldValue,newValue:o.newValue}:null}function Ze(){if(typeof window>"u")return!1;let o=window.clay?.effect||window.hyperclay?.effect;return!!(o&&o.running)}function Qe(){p&&R(p.takeRecords()),x(),A={records:[],primitives:[]}}function et(){let o=A;if(A=null,!o)return[];let u=W(o.records,!0);for(let d of o.primitives)u.push(d);return u}function fe(o,u,{honorPause:d,receipt:f}){if(typeof u!="function")throw new Error("hyper-undo: command() needs a synchronous function");if(A)throw new Error("hyper-undo: command() cannot nest inside another command");if(C)throw new Error("hyper-undo: command() cannot run during undo or redo");if(Ze())throw new Error("hyper-undo: command() cannot run inside a derived-effect context");if(u.constructor&&u.constructor.name==="AsyncFunction")throw new Error("hyper-undo: command() fn must be synchronous; it was declared async");let g=d&&y>0,N=g?null:de();Qe();let S=[],V=null,_;try{try{_=u()}catch(ve){V=ve}if(p&&R(p.takeRecords()),_&&typeof _.then=="function"){try{_.then(void 0,()=>{})}catch{}V||(V=new Error("hyper-undo: command() fn must be synchronous; returned a Promise"))}g||ce(N)}finally{S=et()}let D=g||S.length===0?null:$(o,S);if(V)throw V;return f?D?{id:D.id,label:D.label}:null:D}function tt(o,u){if(!w)throw new Error("hyper-undo: scope not started");return fe(o,u,{honorPause:!1,receipt:!0})}function nt(o){if(o!=="automatic"&&o!=="commands")throw new Error("hyper-undo: setRecording() takes 'automatic' or 'commands'");if(A)throw new Error("hyper-undo: setRecording() cannot change while a command is running");return P===o||(x(),P=o,k("recording",o)),P}function me(o){if(!w||!o||!A&&(y>0||P==="commands"))return;let u=Ye(o);u&&pe(u)}function pe(o){if(A){A.primitives.push(o);return}c.push(o),ue()}function rt(o,{prop:u="value",oldValue:d,newValue:f,read:g,write:N}={}){!o||d===f||me({kind:"value",target:o,prop:u,oldValue:d,newValue:f,read:g,write:N})}function x(){y===0&&p&&R(p.takeRecords()),c.length!==0&&(O(),se())}function ge(){y===0&&p&&R(p.takeRecords()),y++,k("paused",y)}function he(){y!==0&&(y--,y===0&&p&&p.takeRecords(),k("resumed",y))}function ye(o,u){if(je())return ie(),{ok:!1,code:"disconnected"};x();let d=o==="undo"?r:i,f=o==="undo"?i:r,g=d.at(-1);if(!g)return{ok:!1,code:"empty"};if(u&&u!==g.id){let _=be(u)||g;return J(_,o,"stale-entry")}let N;C=!0;try{N=Re(g.certificate,o,{isIgnored:X,ignoreAttribute:le,isProtected:Xe,inScope:dt,liveNodesWithIds:ct})}finally{C=!1}if(!N.ok)return J(g,o,N.code,N.targets);ge();let S;C=!0;try{S=xe(N.operations)}finally{C=!1,he()}if(!S.ok)return k(`${o} failed and was rolled back`,S.error),J(g,o,"replay-error");d.pop(),f.push(g),l=null;let V={ok:!0,entryId:g.id,label:g.label,direction:o};return k(o,g.label),t.emit(o,V),V}function ot(o){return ye("undo",o&&o.entryId)}function it(o){return ye("redo",o&&o.entryId)}function be(o){return r.find(u=>u.id===o)||i.find(u=>u.id===o)||null}function J(o,u,d,f=[]){return l={entryId:o.id,direction:u,code:d,targets:f},k("refused",u,d),t.emit("refused",{entryId:o.id,label:o.label,direction:u,code:d,targets:f}),{ok:!1,code:d,entryId:o.id,label:o.label,direction:u}}function at(o){if(!l||l.entryId!==o)return{ok:!1,code:"not-refused",entryId:o};let u=l.direction,d=u==="undo"?r:i,f=d.at(-1);return!f||f.id!==o?{ok:!1,code:"stale-entry",entryId:o}:(d.pop(),l=null,k("discarded",f.label),t.emit("discard",{entryId:o,label:f.label,direction:u}),{ok:!0,entryId:o,direction:u})}function ut(o){let u=be(o);if(!u||!u.certificate||!u.certificate.replayable)return null;let d=l&&l.entryId===u.id?{direction:l.direction,code:l.code,targets:(l.targets||[]).map(f=>({id:f.id??null,kind:f.kind??null}))}:null;return{id:u.id,label:u.label,timestamp:u.timestamp,refused:d,steps:u.certificate.steps.map(st)}}function st(o){if(o.type==="move")return{kind:"move",node:{id:E(o.node).id},from:{id:E(o.sourceParent).id},to:{id:E(o.destParent).id}};if(o.type==="add"||o.type==="restore")return{kind:o.type==="add"?"add":"remove",parent:{id:E(o.parent).id},nodes:o.nodes.map((N,S)=>({id:E(N).id,html:o.content[S]??null}))};let u=o.primitive,d={id:E(u.target).id},f=u.oldValue??null,g=u.newValue??null;return o.type==="value"?{kind:"value",target:d,prop:u.prop??"value",before:f,after:g}:o.type==="text"?{kind:"text",target:d,before:f,after:g}:o.type==="attr-set"||o.type==="attr-add"||o.type==="attr-remove"?{kind:"attribute",target:d,name:u.name??null,before:f,after:g}:{kind:o.type,target:d,before:f,after:g}}function lt(o){let u=new Set,d=!0;for(;d;){d=!1;for(let f of o)if(!(f.type!=="childList"||!f.removedNodes?.length)&&!(!G(f.target)&&!we(f.target,u)))for(let g of f.removedNodes)u.has(g)||(u.add(g),d=!0)}return f=>!G(f)&&we(f,u)}function we(o,u){for(let d=o;d;d=d.parentNode)if(u.has(d))return!0;return!1}function G(o){let u=n.scope;return!u||!o?!1:o===u||typeof u.contains=="function"&&u.contains(o)}function ke(o){let u=H(),d=u&&u.head;if(!d||!o)return!1;let f=o.nodeType===1?o:o.parentElement;for(;f&&f!==d;f=f.parentElement)if(f.localName==="title")return!1;return f===d}function dt(o){let u=n.scope;return!o||!u?!1:o===u||B()===o||ke(o)?!0:typeof u.contains=="function"?u.contains(o):!1}function ct(o){let u=n.scope;if(!o||o.length===0||typeof u?.querySelectorAll!="function")return[];let d=new Set(o),f=[];for(let g of u.querySelectorAll("[data-id]"))d.has(g.getAttribute("data-id"))&&(X(g)||f.push(g));return f}function ft(){p&&p.takeRecords(),r.length=0,i.length=0,c=[],l=null,O(),k("cleared"),t.emit("clear")}return{_config:n,start:De,stop:ie,commit:He,commitCaptured:Je,discardCaptured:Ge,flush:x,command:tt,setRecording:nt,record:me,recordValue:rt,undo:ot,redo:it,clear:ft,discard:at,describe:ut,pause:ge,resume:he,on:t.on,off:t.off,get canUndo(){return r.length>0},get canRedo(){return i.length>0},get isPaused(){return y>0},get history(){return r.map(o=>{let u={id:o.id,label:o.label,timestamp:o.timestamp};return l&&l.entryId===o.id&&(u.refused={direction:l.direction,code:l.code,targets:l.targets}),u})},get future(){return i.map(o=>{let u={id:o.id,label:o.label,timestamp:o.timestamp};return l&&l.entryId===o.id&&(u.refused={direction:l.direction,code:l.code,targets:l.targets}),u}).reverse()},get scope(){return n.scope}}}var Jt=new Set(["checkbox","radio","button","submit","reset","image","file","range","color"]);function Gt(e){let n=typeof e.composedPath=="function"?e.composedPath():[],t=n.length>0?n:Yt(e.target);for(let r of t){if(!r||r.nodeType!==1)continue;let i=String(r.tagName||"").toUpperCase();if(i==="TEXTAREA"||i==="SELECT"||i==="INPUT"&&!Jt.has(String(r.type||r.getAttribute?.("type")||"text").toLowerCase())||r.isContentEditable===!0)return!0;let a=r.getAttribute&&r.getAttribute("contenteditable");if(a!=null&&String(a).toLowerCase()!=="false")return!0}return!1}function Yt(e){let n=[];for(let t=e;t;t=t.parentNode||(t.host??null))n.push(t);return n}function ze(e,n){let t=r=>{let i=e.scope;if(i&&i.nodeType===1&&i.isConnected===!1){n&&n();return}if(r.defaultPrevented||r.isComposing||r.keyCode===229||Gt(r))return;let a=r.target,s=e._config.shadowKeydownIn;if(a&&a.closest&&Array.isArray(s))for(let h of s)try{if(a.closest(h))return}catch{}if(!(r.metaKey||r.ctrlKey))return;let c=(r.key||"").toLowerCase();if(c==="z"&&!r.shiftKey){r.preventDefault(),r.stopPropagation(),e.undo();return}if(c==="z"&&r.shiftKey){r.preventDefault(),r.stopPropagation(),e.redo();return}if(c==="y"&&!r.shiftKey){r.preventDefault(),r.stopPropagation(),e.redo();return}};return window.addEventListener("keydown",t,!0),()=>window.removeEventListener("keydown",t,!0)}var ne=[".CodeMirror",".cm-editor",".monaco-editor",".ace_editor",".ql-editor",".tiptap",".ProseMirror",".overtype-input","[data-richclay-active]"],m=null,re=null,Ke=null,I=null;function $e(e,n){if(I){if(I.inst===e)return I.cleanup;throw new Error("hyper-undo: another scope already owns the global Cmd+Z binding (bindKeys: true). Stop it first, or use bindKeys: false for additional scopes.")}let t=ze(e,n);return I={inst:e,cleanup:t},t}function Be(e){I&&I.inst===e&&(I.cleanup(),I=null)}var oe={defaults:{shadowKeydownIn:ne},start(e={}){let n=e.scope||(typeof document<"u"?document.body:null);if(!n)throw new Error("hyper-undo: no scope (need document.body or explicit { scope })");if(m){if(re===n)return console.warn("[hyper-undo] start() called again on existing singleton; ignoring new options"),m;throw new Error("hyper-undo: start() called with a different scope while the singleton is already started. Use undo.create({ scope }) for additional scopes, or call undo.stop() first.")}let t={...e,scope:n,shadowKeydownIn:e.shadowKeydownIn||ne};return m=te(t),m.start(),re=n,e.bindKeys!==!1&&(Ke=$e(m,()=>oe.stop())),m},stop(){m&&(Be(m),Ke=null,m.stop(),m=null,re=null)},create(e={}){let n=e.scope||(typeof document<"u"?document.body:null);if(!n)throw new Error("hyper-undo: no scope (need document.body or explicit { scope })");let t={...e,scope:n,shadowKeydownIn:e.shadowKeydownIn||ne},r=te(t),i=null,a={_config:r._config,commit:r.commit,command:r.command,setRecording:r.setRecording,commitCaptured:r.commitCaptured,discardCaptured:r.discardCaptured,flush:r.flush,record:r.record,recordValue:r.recordValue,undo:r.undo,redo:r.redo,clear:r.clear,discard:r.discard,describe:r.describe,pause:r.pause,resume:r.resume,on:r.on,off:r.off,get canUndo(){return r.canUndo},get canRedo(){return r.canRedo},get isPaused(){return r.isPaused},get history(){return r.history},get future(){return r.future},get scope(){return r.scope},start(){e.bindKeys&&(i=$e(r,()=>a.stop())),r.start()},stop(){Be(r),i=null,r.stop()}};return a},commit(e,n){return m?m.commit(e,n):void 0},command(e,n){return m?m.command(e,n):void 0},setRecording(e){return m?m.setRecording(e):void 0},commitCaptured(e){return m?m.commitCaptured(e):void 0},discardCaptured(){return m?m.discardCaptured():void 0},record(e){return m?m.record(e):void 0},recordValue(e,n){return m?m.recordValue(e,n):void 0},undo(e){return m?m.undo(e):void 0},redo(e){return m?m.redo(e):void 0},clear(){return m?m.clear():void 0},discard(e){return m?m.discard(e):void 0},describe(e){return m?m.describe(e):void 0},pause(){return m?m.pause():void 0},resume(){return m?m.resume():void 0},flush(){return m?m.flush():void 0},on(e,n){return m?m.on(e,n):void 0},off(e,n){return m?m.off(e,n):void 0},get canUndo(){return m?m.canUndo:!1},get canRedo(){return m?m.canRedo:!1},get history(){return m?m.history:[]},get future(){return m?m.future:[]},get isPaused(){return m?m.isPaused:!1}};var Zt=oe;return bt(Qt);})();
|
package/package.json
CHANGED
package/src/replay-plan.js
CHANGED
|
@@ -381,7 +381,7 @@ function checkScalar(step, undoing, context) {
|
|
|
381
381
|
}
|
|
382
382
|
|
|
383
383
|
function checkAdd(step, undoing, context) {
|
|
384
|
-
if (!isNode(step.parent) || !
|
|
384
|
+
if (!isNode(step.parent) || !inPlannedScope(step.parent, context)) return refuseStep('target-detached', step.targets)
|
|
385
385
|
if (undoing) {
|
|
386
386
|
if (!sameSequence(listFor(context, step.parent), step.seqAfter)) return refuseStep('structure-changed', step.targets)
|
|
387
387
|
const protectedTargets = protectedWithin(step.nodes, context.isProtected)
|
|
@@ -417,7 +417,7 @@ function checkAdd(step, undoing, context) {
|
|
|
417
417
|
}
|
|
418
418
|
|
|
419
419
|
function checkRestore(step, undoing, context) {
|
|
420
|
-
if (!isNode(step.parent) || !
|
|
420
|
+
if (!isNode(step.parent) || !inPlannedScope(step.parent, context)) return refuseStep('target-detached', step.targets)
|
|
421
421
|
if (undoing) {
|
|
422
422
|
if (!sameSequence(listFor(context, step.parent), step.seqAfter)) return refuseStep('structure-changed', step.targets)
|
|
423
423
|
for (let i = 0; i < step.nodes.length; i++) {
|
|
@@ -441,9 +441,13 @@ function checkRestore(step, undoing, context) {
|
|
|
441
441
|
if (!sameSequence(listFor(context, step.parent), step.seqBefore)) return refuseStep('structure-changed', step.targets)
|
|
442
442
|
const protectedTargets = protectedWithin(step.nodes, context.isProtected)
|
|
443
443
|
if (protectedTargets.length > 0) return refuseStep('protected', protectedTargets)
|
|
444
|
+
// A child an earlier step of this replay moves out (a card moved to another
|
|
445
|
+
// column before its column is removed) is leaving, so the removed subtree is
|
|
446
|
+
// compared without it, as it was when it was recorded.
|
|
447
|
+
const leaving = (node) => context.planned.get(node) === true || (typeof context.isIgnored === 'function' && context.isIgnored(node))
|
|
444
448
|
for (let i = 0; i < step.nodes.length; i++) {
|
|
445
449
|
const node = step.nodes[i]
|
|
446
|
-
if (node.parentNode !== step.parent || durableProjection(node, context.ignoreAttribute, step.parent,
|
|
450
|
+
if (node.parentNode !== step.parent || durableProjection(node, context.ignoreAttribute, step.parent, leaving) !== step.content[i]) return refuseStep('content-changed', step.targets)
|
|
447
451
|
}
|
|
448
452
|
for (const node of step.nodes) {
|
|
449
453
|
context.operations.push({ type: 'remove', node })
|
|
@@ -469,7 +473,7 @@ function checkMove(step, undoing, context) {
|
|
|
469
473
|
const gap = undoing ? step.source.after : step.dest.before
|
|
470
474
|
const slot = undoing ? step.sourceSlot : step.destSlot
|
|
471
475
|
if (!isNode(fromParent) || !isNode(toParent)) return refuseStep('target-detached', step.targets)
|
|
472
|
-
if (!
|
|
476
|
+
if (!inPlannedScope(fromParent, context) || !inPlannedScope(toParent, context)) return refuseStep('target-detached', step.targets)
|
|
473
477
|
if (!sameSequence(fromList, expectedFrom)) return refuseStep('structure-changed', step.targets)
|
|
474
478
|
const toGap = fromParent === toParent ? withoutIdentity(toList, nodeId) : toList
|
|
475
479
|
if (!sameSequence(toGap, gap)) return refuseStep('structure-changed', step.targets)
|
package/src/scope.js
CHANGED
|
@@ -154,7 +154,8 @@ export function createScope(opts) {
|
|
|
154
154
|
// what it wrote there is one entry, so it may own the scope document's head
|
|
155
155
|
// fields too; head's <title> stays with the bracket's own title capture.
|
|
156
156
|
function convertRecords(records, bracketed = false) {
|
|
157
|
-
const
|
|
157
|
+
const removedHere = removedFromScope(records)
|
|
158
|
+
const kept = records.filter((r) => (observedHere(r.target) || removedHere(r.target) || (bracketed && inHead(r.target))) && !shouldIgnore(r.target, config.ignoreAttribute, r))
|
|
158
159
|
return recordsToPrimitives(kept, (node) => shouldIgnore(node, config.ignoreAttribute))
|
|
159
160
|
}
|
|
160
161
|
|
|
@@ -604,6 +605,29 @@ export function createScope(opts) {
|
|
|
604
605
|
return { kind: step.type, target, before, after }
|
|
605
606
|
}
|
|
606
607
|
|
|
608
|
+
// A subtree removed from the scope in this batch is detached by the time the
|
|
609
|
+
// batch is read, yet what happened inside it before the removal (a child moved
|
|
610
|
+
// out of it, say) belongs to the same edit: dropping it would restore the
|
|
611
|
+
// subtree as it was left, not as it was.
|
|
612
|
+
function removedFromScope(records) {
|
|
613
|
+
const removed = new Set()
|
|
614
|
+
let grew = true
|
|
615
|
+
while (grew) {
|
|
616
|
+
grew = false
|
|
617
|
+
for (const r of records) {
|
|
618
|
+
if (r.type !== 'childList' || !r.removedNodes?.length) continue
|
|
619
|
+
if (!observedHere(r.target) && !insideRemoved(r.target, removed)) continue
|
|
620
|
+
for (const node of r.removedNodes) if (!removed.has(node)) { removed.add(node); grew = true }
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return (node) => !observedHere(node) && insideRemoved(node, removed)
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function insideRemoved(node, removed) {
|
|
627
|
+
for (let n = node; n; n = n.parentNode) if (removed.has(n)) return true
|
|
628
|
+
return false
|
|
629
|
+
}
|
|
630
|
+
|
|
607
631
|
function observedHere(node) {
|
|
608
632
|
const root = config.scope
|
|
609
633
|
if (!root || !node) return false
|