@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) • 10.7 kB
JavaScript
!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 s(t,i){return(s=Object.setPrototypeOf||function(t,i){return t.__proto__=i,t})(t,i)}function e(t,i){for(var n=0;n<i.length;n++){var h=i[n];h.enumerable=h.enumerable||!1,h.configurable=!0,"value"in h&&(h.writable=!0),Object.defineProperty(t,h.key,h)}}
/*!
* RyuseiCode.js
* Version : 0.1.17
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
function a(t,i){return-1<t.indexOf(i)}function c(t){return null!==t&&"object"==typeof t}function h(t){return Array.isArray(t)}function r(t){return"string"==typeof t}function l(t){return void 0===t}function o(t,i){return void 0===i&&(i=!1),!h(t)||i&&!h(t[0])?[t]:t}function u(i,t,n){i&&o(t).forEach(function(t){t&&i.classList[n?"add":"remove"](t)})}function f(t,i){if(t)for(var n=Object.keys(t),h=0;h<n.length&&!1!==i(t[n[h]],n[h]);h++);return t}function v(h){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){c(n)&&f(n,function(t,i){l(n[i])||(h[i]=n[i])})}),h}function d(n,t){if(n){if(r(t))return n.getAttribute(t);c(t)&&f(t,function(t,i){null===t?n.removeAttribute(i):n.setAttribute(i,String(t))})}}function p(t,i,n){t=document.createElement(t);return i&&(r(i)||h(i)?u(t,i,!0):d(t,i)),n&&(n=n,o(t).forEach(n.appendChild.bind(n))),t}function y(t,i){return p("div",t,i)}var w=new WeakMap;function m(t,i){t&&(t.cancelable&&t.preventDefault(),i&&t.stopPropagation())}var g="ryuseicode";var x=Math.max,b=Math.min;function k(t,i){return t[0]-i[0]||t[1]-i[1]}var _="is-active",A="change",M="changed",T="keymap",C=[{id:"matchCase",icon:"matchCase",click:"toggleMatchCase",checkbox:!0},{id:"wholeWord",icon:"word",click:"toggleWholeWord",checkbox:!0},{id:"regexp",icon:"regexp",click:"toggleRegExp",checkbox:!0},{id:"prevMatch",icon:"arrowUp",click:"prev"},{id:"nextMatch",icon:"arrowDown",click:"next"}],R=[{id:"replace",click:"replace"},{id:"replaceAll",click:"replaceAll"}],W=g+"__search",F=g+"__replace",j="search",z="match",E="active-match",O={hideButtons:[]},N={search:"Search",searchToolbar:"Search/Replace Toolbar",wholeWord:"Match Whole Word",prevMatch:"Previous Match",nextMatch:"Next Match",replace:"Replace",replaceAll:"Replace All",matchCase:"Match Case",regexp:"Regex",noResults:"No results"},S={regexp:["m15 2a1.5 1.5 0 0 0-1.5 1.5v3.4l-3.1-1.7a1.5 1.5 0 0 0-2 0.57 1.5 1.5 0 0 0 0.58 2l3 1.7-3 1.7a1.5 1.5 0 0 0-0.58 2 1.5 1.5 0 0 0 2 0.58l3.1-1.7v3.4a1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5v-3.4l3.1 1.7a1.5 1.5 0 0 0 2-0.58 1.5 1.5 0 0 0-0.58-2l-3-1.7 3-1.7a1.5 1.5 0 0 0 0.58-2 1.5 1.5 0 0 0-2-0.57l-3.1 1.7v-3.4a1.5 1.5 0 0 0-1.5-1.5zm-9.7 13c-1.8 0-3.3 1.5-3.3 3.3s1.5 3.3 3.3 3.3c1.8 0 3.3-1.5 3.3-3.3s-1.5-3.3-3.3-3.3z"],word:["m22 4h-2.95l-2.37 11.3-2.56-11.3h-3.94l-2.44 11.3-2.51-11.3h-3.16l4.11 16h2.87l3.02-11.8 3.04 11.8h2.9z"],matchCase:["m6.9 4.2c-3.7 0-5.6 2-5.6 6v3.5c0 4 1.8 6 5.5 6 1.7 0 3-0.42 4-1.3 0.97-0.88 1.5-2.1 1.5-3.8v-0.15h-2.8v0.18c0 0.77-0.22 1.4-0.66 1.8s-1.1 0.66-1.9 0.66c-0.92 0-1.6-0.26-2-0.84-0.44-0.55-0.64-1.5-0.64-2.7v-3.3c0-1.3 0.2-2.2 0.62-2.8s1.1-0.86 2-0.86c0.81 0 1.5 0.24 1.9 0.68 0.44 0.46 0.68 1.1 0.68 1.8v0.13h2.9v-0.15c0-1.6-0.51-2.8-1.5-3.7-0.99-0.88-2.3-1.3-4-1.3zm11 3.8c-1.5 0-2.6 0.4-3.4 1.2-0.84 0.79-1.2 2-1.2 3.6v2.1c0 1.6 0.4 2.9 1.2 3.6 0.84 0.79 2 1.2 3.4 1.2 1.4 0 2.5-0.35 3.3-1.1 0.79-0.73 1.2-1.7 1.2-3v-0.15h-2.6v0.13c0 0.53-0.18 0.95-0.51 1.2-0.33 0.31-0.77 0.44-1.3 0.44-0.64 0-1.1-0.18-1.5-0.55-0.35-0.35-0.51-0.97-0.51-1.8v-2.1c0-1.6 0.66-2.4 2-2.4 0.57 0 1 0.15 1.3 0.46 0.33 0.31 0.51 0.75 0.51 1.3v0.13h2.6v-0.22c0-1.3-0.4-2.3-1.2-3-0.79-0.7-1.9-1.1-3.3-1.1z"]},P={search:["F",!0],searchNext:["F3"],searchPrev:["F3",!1,!0],replace:["F",!0,!0]},t=function(n){var t;function i(t){t=n.call(this,t)||this;return t.t=[],t.i=-1,t.addIcons(S),t.addI18n(N),t.addKeyBindings(P),t}h=n,(t=i).prototype=Object.create(h.prototype),s(t.prototype.constructor=t,h);var h=i.prototype;return h.mount=function(t){var h,s,e,c,r,o,a;function i(){c&&f();for(var t=arguments.length,i=new Array(t),n=0;n<t;n++)i[n]=arguments[n];a=u.bind.apply(u,[this].concat(i)),o||(l(o)&&e?a():o=r?requestAnimationFrame(a):setTimeout(a,s))}function u(){for(var t=arguments.length,i=new Array(t),n=0;n<t;n++)i[n]=arguments[n];h.apply(this,i),f()}function f(){(r?cancelAnimationFrame:clearTimeout)(o),o=null}(this.n=this.require("Toolbar"))&&(n.prototype.mount.call(this,t),this.h=this.getOptions("search",O),this.s=(h=this.e.bind(this),s=10,i.cancel=f,i.invoke=function(){o&&a()},i),this.c(),this.n.register(j,this.r,this.i18n.searchToolbar),this.o())},h.c=function(){var i=this,t=this.n,n=y(),h=y(W,n),s=y(F,n);this.searchField=t.createField({id:"search",tabindex:1},h),this.replaceField=t.createField({id:"replace",tabindex:1},s);var e=y("ryuseicode__search__controls",h),c=y("ryuseicode__replace__controls",s),r=C.filter(function(t){return!a(i.h.hideButtons,t.id)}),o=R.filter(function(t){return!a(i.h.hideButtons,t.id)});this.a=v(t.createButtons(r,e,this),t.createButtons(o,c,this)),this.h.hideMatchCount||(this.u=p("span","ryuseicode__search__matches",e)),this.r=n,this.f=h,this.l=s},h.o=function(){var h=this,i=this.searchField;this.on(T+":search",function(t,i){h.show(!h.options.keymap.replace),m(i)}),this.on(T+":replace",function(t,i){h.show(!0),m(i)}),this.bind(i,"input",this.v,this),this.bind(i,"keydown",this.d,this),this.bind(this.replaceField,"keydown",this.p,this),this.on("toolbar:opened",function(t,i,n){n!==j&&h.clear()}),this.on("toolbar:closed",this.clear,this),this.on([M,"synced"],function(){var t=i.value;h.y()&&t&&h.s(t,h.i)}),this.on("readOnly",function(t,i){h.y()&&h.w(!i)})},h.d=function(t){if("Enter"===t.key)return this.next(),void m(t);this.m(t)},h.p=function(t){if("Enter"===t.key)return this.replace(),void m(t);this.m(t)},h.m=function(t){var i=t.key.toUpperCase(),n=this.Keymap,h=n.matches.bind(n,t),s=h("searchNext"),n=h("searchPrev");s||n?(this[n?"prev":"next"](),m(t)):h("search")?(this.show(!1),m(t)):h("replace")?(this.show(!0),m(t)):t.ctrlKey?"A"!==i&&"X"!==i&&"C"===i&&m(t):t.altKey&&m(t)},h.v=function(){var t=this.searchField.value;t?this.s(t):(this.clear(),this.g())},h.e=function(t,i){void 0===t&&(t=this.searchField.value);var n=this.Range;try{h=this.x&&t?new RegExp(t):t}catch(t){return}var h,t=this.Code.search(h,!this.b,this.k,1e4);this.clear(),n.register(z,t),this.t=t,l(i)||i<0?(this.i=-1,this.next()):(this.i=(h=i,n=0,i=t.length-1,t=b(n,i),i=x(n,i),b(x(t,h),i)),this.activate(this.i)),this._(),this.g()},h.A=function(t){this.e(void 0,t)},h._=function(){var t;this.u&&(t=(t=this.t.length)?1e4<t?1e4+"+":this.i+1+"/"+t:this.i18n.noResults,function(t,i){if(t){if(l(i))return t.textContent;t.textContent=i}}(this.u,t))},h.g=function(){var i=this;["prevMatch","nextMatch","replace","replaceAll"].forEach(function(t){t=i.a[t];t&&(t.disabled=!i.t.length)})},h.M=function(t){t=this.t[t];t&&this.View.jump(t.start[0],!0)},h.T=function(t){var i=this.t.length,t=this.i+(t?-1:1);i<=t?t=0:t<0&&(t=i-1),this.activate(t),this.M(t),this.i=t,this._()},h.C=function(t,i){u(t,_,i),d(t,{"aria-checked":i})},h.w=function(t){u(this.l,_,t&&!this.Editor.readOnly&&!this.h.hideReplace)},h.y=function(){return this.n.isActive(j)},h.toggleMatchCase=function(t){void 0===t&&(t=!this.b),this.C(this.a.matchCase,this.b=t),this.e()},h.toggleRegExp=function(t){void 0===t&&(t=!this.x),this.C(this.a.regexp,this.x=t),this.e()},h.toggleWholeWord=function(t){void 0===t&&(t=!this.k),this.C(this.a.wholeWord,this.k=t),this.e()},h.activate=function(t){var i=this.t[t];i&&((t=this.Range).clear(E),t.register(E,[i]))},h.next=function(){this.T(!1)},h.prev=function(){this.T(!0)},h.replace=function(t,i){var n=this;void 0===t&&(t=this.replaceField.value),void 0===i&&(i=this.i);var h,s,e=this.t,c=e[i];c&&(h=this.Selection,s=c.start,c=c.end,e=e[i+1],h.update(s,s,!0),this.emit(A,"replace"),this.M(i),this.Code.replaceRange(s,c,t),this.Sync.sync(s[0],c[0]),this.emit(M,"replace"),this.A(i),e&&(this.i=this.R(e),this.activate(this.i)),this.W=setTimeout(function(){n.M(n.i)},20))},h.R=function(t){for(var i=this.t,n=0;n<i.length;n++)if(!k(i[n].start,t.start)&&!k(i[n].end,t.end))return n;return-1},h.replaceAll=function(i){var n=this;void 0===i&&(i=this.replaceField.value);var t,h=this.t;h.length&&(this.emit(A),h.forEach(function(t){n.Code.replaceRange(t.start,t.end,i)}),t=h[h.length-1].end[0],this.View.jump(t),this.Sync.sync(h[0].start[0],t),this.clear(),this.emit(M))},h.show=function(t){var i=this.Selection,n=this.searchField;this.w(t),i.isCollapsed()||i.isMultiline()||(n.value=i.toString()),this.n.show(j),this.A()},h.clear=function(){var t=this.Range;t.clear(z),t.clear(E),this.t=[],this._(),this.s.cancel(),clearTimeout(this.W)},i}(function(){function t(t){this.Editor=t,this.event=t.event,this.options=t.options,this.language=t.language}var i,n,h=t.prototype;return h.mount=function(t){var n=this;this.elements=t,f(this.Editor.Components,function(t,i){n[i]=t})},h.destroy=function(){!function i(n,t,h){"function"==typeof h?t.split(" ").forEach(function(t){n.removeEventListener(t,h)}):(t=w.get(h))&&(t.forEach(function(t){i(t.elm,t.events,t.callback)}),w.delete(h))}(null,"",this)},h.on=function(t,i,n,h){this.event.on(t,n?i.bind(n):i,this,h)},h.off=function(t){this.event.off(t,this)},h.emit=function(t){for(var i,n=arguments.length,h=new Array(1<n?n-1:0),s=1;s<n;s++)h[s-1]=arguments[s];(i=this.event).emit.apply(i,[t].concat(h))},h.bind=function(t,i,n,h){var s,e,c,r;s=t,e=i,c=h?n.bind(h):n,r=this,e.split(" ").forEach(function(t){s.addEventListener(t,c),r&&((t=w.get(r)||[]).push({elm:s,events:e,callback:c}),w.set(r,t))})},h.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},h.invoke=function(t,i){for(var n,h=arguments.length,s=new Array(2<h?h-2:0),e=2;e<h;e++)s[e-2]=arguments[e];return(n=this.Editor).invoke.apply(n,[t,i].concat(s))},h.require=function(t){return this.Editor.require(t)},h.addIcons=function(t){var i=this.options;i.icons=v({},t,i.icons)},h.addI18n=function(t){var i=this.options;i.i18n=v({},t,i.i18n)},h.addKeyBindings=function(t){var i=this.options;i.keymap=v({},t,i.keymap)},h.getOptions=function(t,i){t=this.options[t];return l(t)||!0===t?i||{}:c(t)?v({},i,t):void function(t,i){if(void 0===i&&(i=""),!t)throw new Error("["+g+"] "+i)}(!1)},i=t,(h=[{key:"lines",get:function(){return this.Code.Lines}},{key:"i18n",get:function(){return this.options.i18n}}])&&e(i.prototype,h),n&&e(i,n),t}());"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.compose({Search:t})});