UNPKG

@ryusei/code

Version:

<div align="center"> <a href="https://code.ryuseijs.com"> <img alt="RyuseiCode" src="https://code.ryuseijs.com/images/svg/logo.svg" width="70"> </a>

8 lines (7 loc) 5.14 kB
!function(t,i){"function"==typeof define&&define.amd?define([],i):"undefined"!=typeof exports?i():(i(),t.unknown={})}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(){"use strict";function r(t,i){return(r=Object.setPrototypeOf||function(t,i){return t.__proto__=i,t})(t,i)}function s(t,i){for(var n=0;n<i.length;n++){var s=i[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function h(t,i,n){return i&&s(t.prototype,i),n&&s(t,n),t} /*! * RyuseiCode.js * Version : 0.1.17 * License : MIT * Copyright: 2021 Naotoshi Fujita */function o(t){return null!==t&&"object"==typeof t}function v(t){return void 0===t}function e(t,i){if(t)for(var n=Object.keys(t),s=0;s<n.length&&!1!==i(t[n[s]],n[s]);s++);return t}function n(s){for(var t=arguments.length,i=new Array(1<t?t-1:0),n=1;n<t;n++)i[n-1]=arguments[n];return i.forEach(function(n){o(n)&&e(n,function(t,i){v(n[i])||(s[i]=n[i])})}),s}var u=new WeakMap;function f(t,i){return h=t,o=i,u=!(e=!1),n.cancel=r,n.invoke=function(){c&&a()},n;function n(){u&&r();for(var t=arguments.length,i=new Array(t),n=0;n<t;n++)i[n]=arguments[n];a=s.bind.apply(s,[this].concat(i)),c||(v(c)&&e?a():c=f?requestAnimationFrame(a):setTimeout(a,o))}function s(){for(var t=arguments.length,i=new Array(t),n=0;n<t;n++)i[n]=arguments[n];h.apply(this,i),r()}function r(){(f?cancelAnimationFrame:clearTimeout)(c),c=null}var h,o,e,u,f,c,a}function c(t,i){return t[0]-i[0]||t[1]-i[1]}var a="change",d="changed",l="keymap",y={limit:100,debounce:300},w={undo:["Z",!0,!1],redo:["Z",!0,!0]},p="history",t=function(i){var t;function n(t){t=i.call(this,t)||this;return t.t=[],t.i=0,t.addKeyBindings(w),t}s=i,(t=n).prototype=Object.create(s.prototype),r(t.prototype.constructor=t,s);var s=n.prototype;return s.mount=function(t){i.prototype.mount.call(this,t),this.n=this.getOptions("history",y),this.s=f(this.r.bind(this),this.n.debounce),this.h()},s.h=function(){var s=this;this.on(a,this.o,this),this.on(d,this.e,this),this.on(l+":undo "+l+":redo",function(t,i,n){i.preventDefault(),s.Editor.readOnly||s[n]()}),this.on("reset",function(){s.t.length=0})},s.u=function(){return{range:this.Selection.get(),value:this.Code.value,length:this.lines.length}},s.f=function(t){var i=t.range,n=t.length,s=i.start,i=i.end;this.emit(a,p),this.Code.value=t.value,this.Sync.sync(0,n-1,s[0]),this.Selection.set(s,i),this.emit(d,p),this.emit("history:restored",t)},s.r=function(t){var i=this.t[this.i];i&&this.c(i,t)||(this.t.push(t),this.length>this.n.limit&&this.t.shift(),this.i=this.length-1,this.emit("history:pushed",t),this.s.cancel())},s.c=function(t,i){return t.value===i.value&&!c(t.range.start,i.range.start)&&!c(t.range.end,i.range.end)},s.a=function(){return this.i!==this.length-1},s.o=function(t,i){var n;i!==p&&(n=this.t,this.a()&&n.splice(this.i+1,n.length),this.Selection.isCollapsed()&&this.length&&"replace"!==i||this.r(this.u()))},s.e=function(t,i){this.Input.composing||i===p||("input"===i?this.s(this.u()):this.r(this.u()))},s.undo=function(){this.s.invoke(),0<this.i&&this.i<this.length&&this.f(this.t[--this.i])},s.redo=function(){this.i<this.length-1&&this.f(this.t[++this.i])},h(n,[{key:"length",get:function(){return this.t.length}}]),n}(function(){function t(t){this.Editor=t,this.event=t.event,this.options=t.options,this.language=t.language}var i=t.prototype;return i.mount=function(t){var n=this;this.elements=t,e(this.Editor.Components,function(t,i){n[i]=t})},i.destroy=function(){!function i(n,t,s){"function"==typeof s?t.split(" ").forEach(function(t){n.removeEventListener(t,s)}):(t=u.get(s))&&(t.forEach(function(t){i(t.elm,t.events,t.callback)}),u.delete(s))}(null,"",this)},i.on=function(t,i,n,s){this.event.on(t,n?i.bind(n):i,this,s)},i.off=function(t){this.event.off(t,this)},i.emit=function(t){for(var i,n=arguments.length,s=new Array(1<n?n-1:0),r=1;r<n;r++)s[r-1]=arguments[r];(i=this.event).emit.apply(i,[t].concat(s))},i.bind=function(t,i,n,s){var r,h,o,e;r=t,h=i,o=s?n.bind(s):n,e=this,h.split(" ").forEach(function(t){r.addEventListener(t,o),e&&((t=u.get(e)||[]).push({elm:r,events:h,callback:o}),u.set(e,t))})},i.getLanguage=function(t){t=t||this.Selection.focus;var i=this.language,t=this.lines.getInfoAt(t);return t&&t.language&&i.use&&i.use[t.language]?i.use[t.language].config:i},i.invoke=function(t,i){for(var n,s=arguments.length,r=new Array(2<s?s-2:0),h=2;h<s;h++)r[h-2]=arguments[h];return(n=this.Editor).invoke.apply(n,[t,i].concat(r))},i.require=function(t){return this.Editor.require(t)},i.addIcons=function(t){var i=this.options;i.icons=n({},t,i.icons)},i.addI18n=function(t){var i=this.options;i.i18n=n({},t,i.i18n)},i.addKeyBindings=function(t){var i=this.options;i.keymap=n({},t,i.keymap)},i.getOptions=function(t,i){t=this.options[t];return v(t)||!0===t?i||{}:o(t)?n({},i,t):void function(t,i){if(void 0===i&&(i=""),!t)throw new Error("[ryuseicode] "+i)}(!1)},h(t,[{key:"lines",get:function(){return this.Code.Lines}},{key:"i18n",get:function(){return this.options.i18n}}]),t}());"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.compose({History:t})});