@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>
9 lines (8 loc) • 7.13 kB
JavaScript
!function(t,n){"function"==typeof define&&define.amd?define([],n):"undefined"!=typeof exports?n():(n(),t.unknown={})}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(){"use strict";function r(t,n){return(r=Object.setPrototypeOf||function(t,n){return t.__proto__=n,t})(t,n)}function o(t,n){for(var i=0;i<n.length;i++){var e=n[i];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}
/*!
* RyuseiCode.js
* Version : 0.1.17
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
function u(t){return null!==t&&"object"==typeof t}function e(t){return Array.isArray(t)}function s(t){return"function"==typeof t}function f(t){return"string"==typeof t}function h(t){return void 0===t}function c(t,n){return void 0===n&&(n=!1),!e(t)||n&&!e(t[0])?[t]:t}function a(t,n){var i,e;n=n,e=!0,(i=t)&&c(n).forEach(function(t){t&&i.classList[e?"add":"remove"](t)})}function d(t,n){if(t)for(var i=Object.keys(t),e=0;e<i.length&&!1!==n(t[i[e]],i[e]);e++);return t}function v(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),i=1;i<t;i++)n[i-1]=arguments[i];return n.forEach(function(i){u(i)&&d(i,function(t,n){h(i[n])||(e[n]=i[n])})}),e}function l(t,n,i){t=document.createElement(t);return n&&(f(n)||e(n)?a:function(i,t){if(i){if(f(t))return i.getAttribute(t);u(t)&&d(t,function(t,n){null===t?i.removeAttribute(n):i.setAttribute(n,String(t))})}})(t,n),i&&(i=i,c(t).forEach(i.appendChild.bind(i))),t}var g=new WeakMap;function p(t,n){t&&(t.cancelable&&t.preventDefault(),n&&t.stopPropagation())}Math.min;var y=Math.max;Math.floor,Math.ceil,Math.abs,Math.round;var b="\n",w="change",M="changed",k="keymap",T={help:!0,deepIndent:!0},j={indentNotice:"Indent/Move Focus",indentDisabled:"Inserting indents by the Tab is currently disabled. You can toggle it by %s."},m={indent:["Tab"],unindent:["Tab",!1,!0],toggleTabMode:["M",!0]},A="tab-notice",t=function(n){var t;function i(t){t=n.call(this,t)||this;return t.addI18n(j),t.addKeyBindings(m),t}e=n,(t=i).prototype=Object.create(e.prototype),r(t.prototype.constructor=t,e);var e=i.prototype;return e.mount=function(t){(this.t=this.require("Dialog"))&&(n.prototype.mount.call(this,t),this.n=this.options.indent,this.i=this.getOptions("indentation",T),this.e="load"!==this.i.activation,this.r(),this.o())},e.setDisabled=function(t){this.e=t,i.u=!0},e.o=function(){var i,e=this;this.on("focus",function(t,n){"pointer"!==n||i||e.setDisabled(!1),i=!0}),this.on(k+":indent "+k+":unindent",function(t,n,i){e.e||("indent"===i?e.s():e.f(),p(n))}),this.on(k+":toggleTabMode",function(t,n){e.setDisabled(!e.e),p(n)}),this.on("newline",function(){e.h(),e.i.deepIndent&&e.c()}),this.on("keydown",this.a,this)},e.a=function(t,n){if(this.i.help&&!i.u&&"Tab"===n.key)return this.t.show(A),i.u=!0,void p(n);this.d(n)},e.r=function(){var t=this,n=this.i18n,i=l("div",void 0,void 0);!function(t,n){if(t){if(h(n))return t.innerHTML;t.innerHTML!==n&&(t.innerHTML=n)}}(i,function(t){for(var n=0;n<(arguments.length<=1?0:arguments.length-1);n++)t=t.replace("%s",String(n+1<1||arguments.length<=n+1?void 0:arguments[n+1]));return t}("<p>"+n.indentDisabled+"</p>","<strong>"+this.Keymap.getShortcut("toggleTabMode")+"</strong>")),this.t.register(A,i,n.indentNotice,[{id:"activate",click:function(){t.setDisabled(!1),t.t.hide()}},"confirm"])},e.s=function(){var t,n=this.Input,i=this.Selection,e=this.n,r=this.n.length;i.isCollapsed()?n.apply({type:"indent",insertion:e,offset:r}):(this.emit(w),n=(t=i.get()).start,t=t.end,this.Code.replaceLinesBy(n[0],t[0],function(t){return e+t}),this.Sync.sync(n[0],t[0]),i.set([n[0],n[1]+r],[t[0],t[1]+r]),this.emit(M))},e.f=function(){var r,t,o=this,u=this.n,n=this.Selection.get(),i=n.start,e=n.end,s=0,f=0;this.Code.replaceLinesBy(i[0],e[0],function(t,n,i){var e=t.match(new RegExp("^("+u+"| {0,"+u.length+"})"));return e&&(e=e[0],t=t.replace(e,""),0===n&&(o.emit(w),s-=e.length),n===i.length-1&&(f-=e.length),r=!0),t}),r&&(t=y(i[1]+s,0),n=y(e[1]+f,0),this.Sync.sync(i[0],e[0]),this.Selection.set([i[0],t],[e[0],n]),this.emit(M))},e.h=function(){var t=this.Input,n=this.lines[t.row].getIndent();n&&t.set("newline",{value:t.before+b+n+t.after.replace(/^[ \t]+/,""),position:[t.row+1,n.length]})},e.c=function(){var t,n,i,e=this.v();-1<e&&this.l(e)&&(t=this.Input,n=this.n,i=this.lines[t.row].getIndent(),e=b+i+n+(this.g(e)?b+i:""),t.set("indentDeep",{key:"Enter",insertion:e,position:[t.row+1,i.length+n.length]}))},e.v=function(){for(var t=this.p(),n=0;n<t.length;n++){var i=t[n];if(s(i[0]))return i[0](this.Editor)?n:-1;var e=this.Input;if(i[0].test(e.before.trim()))return n}return-1},e.l=function(t){t=this.p()[t],t=t&&t[2];return s(t)?t(this.Editor):!t||this.Scope.isIn(t)},e.g=function(t){var n=this.p()[t],t=n&&n[1];if(!t)return!1;if(s(t))return t(this.Editor);n=this.Input;return t.test(n.after.trim())},e.d=function(t){var n,i,e,r,o,u=this.Selection;"Backspace"!==t.key||!u.isCollapsed()||(o=(r=this.lines)[i=(n=u.get().start)[0]-1])&&(e=o.getIndent(),r=r[n[0]].getIndent(),e&&e===r&&n[1]===r.length&&(this.emit(w),o=[i,o.text.length],this.Code.replaceRange(o,n,""),this.Sync.sync(i,n[0]),u.set(o),this.emit(M),p(t)))},e.p=function(){return this.getLanguage().indent||[]},i}(function(){function t(t){this.Editor=t,this.event=t.event,this.options=t.options,this.language=t.language}var n,i,e=t.prototype;return e.mount=function(t){var i=this;this.elements=t,d(this.Editor.Components,function(t,n){i[n]=t})},e.destroy=function(){!function n(i,t,e){s(e)?t.split(" ").forEach(function(t){i.removeEventListener(t,e)}):(t=g.get(e))&&(t.forEach(function(t){n(t.elm,t.events,t.callback)}),g.delete(e))}(null,"",this)},e.on=function(t,n,i,e){this.event.on(t,i?n.bind(i):n,this,e)},e.off=function(t){this.event.off(t,this)},e.emit=function(t){for(var n,i=arguments.length,e=new Array(1<i?i-1:0),r=1;r<i;r++)e[r-1]=arguments[r];(n=this.event).emit.apply(n,[t].concat(e))},e.bind=function(t,n,i,e){var r,o,u,s;r=t,o=n,u=e?i.bind(e):i,s=this,o.split(" ").forEach(function(t){r.addEventListener(t,u),s&&((t=g.get(s)||[]).push({elm:r,events:o,callback:u}),g.set(s,t))})},e.getLanguage=function(t){t=t||this.Selection.focus;var n=this.language,t=this.lines.getInfoAt(t);return t&&t.language&&n.use&&n.use[t.language]?n.use[t.language].config:n},e.invoke=function(t,n){for(var i,e=arguments.length,r=new Array(2<e?e-2:0),o=2;o<e;o++)r[o-2]=arguments[o];return(i=this.Editor).invoke.apply(i,[t,n].concat(r))},e.require=function(t){return this.Editor.require(t)},e.addIcons=function(t){var n=this.options;n.icons=v({},t,n.icons)},e.addI18n=function(t){var n=this.options;n.i18n=v({},t,n.i18n)},e.addKeyBindings=function(t){var n=this.options;n.keymap=v({},t,n.keymap)},e.getOptions=function(t,n){t=this.options[t];return h(t)||!0===t?n||{}:u(t)?v({},n,t):void function(t,n){if(void 0===n&&(n=""),!t)throw new Error("[ryuseicode] "+n)}(!1)},n=t,(e=[{key:"lines",get:function(){return this.Code.Lines}},{key:"i18n",get:function(){return this.options.i18n}}])&&o(n.prototype,e),i&&o(n,i),t}());"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.compose({Indentation:t})});