-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundo.min.js
More file actions
1 lines (1 loc) · 3.65 KB
/
undo.min.js
File metadata and controls
1 lines (1 loc) · 3.65 KB
1
"use strict"(function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof module&&module.exports?module.exports=t():e.undo=t()}(this,function(){function m(t,e,n){this.disconnect=function(){for(var e in n)n.hasOwnProperty(e)&&t.removeEventListener(e,n[e])}}var r=async function(r){r.tagName.toUpperCase();var i=document.queryCommandEnabled("undo"),d=document.queryCommandEnabled("redo");return this.isPlainText(r)?new Promise(e=>{i&&d&&e(!1);var t=r.selectionStart,n=r.selectionEnd,o=r.selectionDirection;i||(r.selectionStart=r.selectionEnd=0,document.execCommand("insertText",!1,""),r.selectionStart=0,r.selectionEnd=1,document.execCommand("cut"),r.selectionStart=t,r.selectionEnd=n,r.selectionDirection=o),d||(document.execCommand("insertText",!1,""),document.execCommand("undo")),e(!0)}):new Promise(a=>{i&&d&&a(!1),new MutationObserver(function(e,t){var n=[];e:for(var o=0;o<e.length;o++){var r=e[o].addedNodes;if(r)for(var i=0;i<r.length;i++)if(8==r[i].nodeType&&!r[i].data&&(n.push(r[i]),2<=n.length))break e}if(t.disconnect(),n.length<1)return a(!1),!1;for(o=0;o<n.length;o++)n[o].remove();a(!0)}).observe(r,{childList:!0,subtree:!0}),i||document.execCommand("insertHTML",!1,"\x3c!----\x3e"),d||(document.execCommand("insertHTML",!1,"\x3c!----\x3e"),document.execCommand("undo"))})};return new function(){var i=new Set;this.observe=function(e,t){(t=t||{}).captureAll=!("captureAll"in t&&!t.captureAll);var n=d.bind(this,e,t),o=l.bind(this,e,t),r=c.bind(this,e,t),i=u.bind(this,e,t),a=s.bind(this,e,t);return e.addEventListener("beforeinput",n,!1),e.addEventListener("input",o,!1),e.addEventListener("focusin",r,!1),e.addEventListener("keydown",i,!1),e.addEventListener("keyup",a,!1),new m(e,0,{beforeinput:n,input:o,focusin:r,keydown:i,keyup:a})},this.isPlainText=function(e){if(e.tagName){var t=e.tagName.toUpperCase();if("INPUT"==t||"TEXTAREA"==t)return!0;if("plaintext-only"==e.contentEditable)return!0;if("inherit"==e.contentEditable){e=e.parentElement&&e.parentElement.closest("[contenteditable]");if(e&&"plaintext-only"==e.contentEditable)return!0}return!1}return!1};var d=function(e,t){var n=window.event;if(!n.isTrusted&&!t.allowUntrusted)return!1;var o,r=n.inputType;n.target.tagName.toUpperCase();"historyUndo"!=r&&"historyRedo"!=r||("historyRedo"==r&&!t.preventDefault&&t.captureAll&&(o=n.target,new MutationObserver(function(e,t){var n=[];e:for(var o=0;o<e.length;o++){var r=e[o].addedNodes;if(r)for(var i=0;i<r.length;i++)if(8==r[i].nodeType&&!r[i].data&&(n.push(r[i]),2<=n.length))break e}if(t.disconnect(),n.length<1)return!1;for(o=0;o<n.length;o++)n[o].remove()}).observe(o,{childList:!0,subtree:!0})),r=new CustomEvent("before"+("historyUndo"==r?"undo":"redo"),{detail:{originalEvent:n,shortcut:a==("historyUndo"==r?"z":"y")}}),r=n.target.dispatchEvent(r),i.add(n.target),(t.preventDefault||r)&&(n.preventDefault(),l.call(this,e,t,!0)))},a=null,u=function(e,t){var n=window.event;a=n.key},s=function(e,t){lastkey=null},c=function(e,t){var n=window.event;document.queryCommandEnabled("undo")&&document.queryCommandEnabled("redo")||!t.captureAll||r.call(this,n.target)},l=async function(e,t,n){var o=window.event;if(!o.isTrusted&&!t.allowUntrusted)return!1;if(!i.has(o.target))return!1;i.delete(o.target),this.isPlainText(o.target)&&!n&&t.captureAll&&o.target.selectionStart&&o.target.selectionEnd&&""==o.target.value[0]&&document.execCommand("undo");n=o.inputType;n=t.captureAll?(t=await r.call(this,o.target),new CustomEvent("historyUndo"==n?"undo":"redo",{detail:{modified:t,originalEvent:o,shortcut:a==("historyUndo"==n?"z":"y")}})):new CustomEvent("historyUndo"==n?"undo":"redo",{detail:{originalEvent:o,shortcut:a==("historyUndo"==n?"z":"y")}}),o.target.dispatchEvent(n)}}}));