@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.96 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 o(t,i){t.prototype=Object.create(i.prototype),n(t.prototype.constructor=t,i)}function n(t,i){return(n=Object.setPrototypeOf||function(t,i){return t.__proto__=i,t})(t,i)}function r(t,i){for(var n=0;n<i.length;n++){var o=i[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}
/*!
* RyuseiCode.js
* Version : 0.1.17
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
var e="ryuseicode",c=e+"__button",u="is-active";function s(t){return null!==t&&"object"==typeof t}function f(t){return Array.isArray(t)}function h(t){return"string"==typeof t}function a(t){return void 0===t}function l(t,i){return void 0===i&&(i=!1),!f(t)||i&&!f(t[0])?[t]:t}function d(i,t,n){i&&l(t).forEach(function(t){t&&i.classList[n?"add":"remove"](t)})}function v(t,i){d(t,i,!0)}function y(t,i){l(i).forEach(t.appendChild.bind(t))}function w(t,i){if(t)for(var n=Object.keys(t),o=0;o<n.length&&!1!==i(t[n[o]],n[o]);o++);return t}function _(o){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){s(n)&&w(n,function(t,i){a(n[i])||(o[i]=n[i])})}),o}function b(n,t){if(n){if(h(t))return n.getAttribute(t);s(t)&&w(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&&(h(i)||f(i)?v:b)(t,i),n&&y(n,t),t}function g(t,i){return p("div",t,i)}var k=new WeakMap;function m(t,i){return t.querySelector(i)}function x(t){l(t).forEach(function(t){t&&t.parentNode&&t.parentNode.removeChild(t)})}function A(t,i){d(t,i,!1)}function j(t,i){if(t){if(a(i))return t.textContent;t.textContent=i}}function D(t,i){if(void 0===i&&(i=""),!t)throw new Error("["+e+"] "+i)}var E={Up:"ArrowUp",Down:"ArrowDown",Right:"ArrowRight",Left:"ArrowLeft",Del:"Delete",Esc:"Escape",Spacebar:" "};var O="0 0 24 24";function B(t,i){t=document.createElementNS("http://www.w3.org/2000/svg",t);return b(t,i),t}var t=function(i){function t(){var t=i.apply(this,arguments)||this;return t.groups={},t}o(t,i);var n=t.prototype;return n.mount=function(t){i.prototype.mount.call(this,t),this.create(),this.listen()},n.create=function(){D(!1)},n.listen=function(){this.bind(window,"keydown",this.escape,this)},n.escape=function(t){var i,n;this.isActive()&&"Escape"===(n=t.key,E[n]||n)&&(this.hide(),(t=t)&&(t.cancelable&&t.preventDefault(),i&&t.stopPropagation()))},n.append=function(t){y(this.wrapper,this.groups[t].elm)},n.autoFocus=function(t){t=this.groups[t].elm,t=m(t,"[tabindex]")||m(t,"input, button");t&&(t.focus(),t instanceof HTMLInputElement&&t.select())},n.createCloseButton=function(t){var i=this.createButtons({id:"close",icon:"close",click:"hide"},null,this).close;return b(i,t),i},n.createButtons=function(t,o,r,e){var u=this,c={};return l(t).forEach(function(i){var t=u.createButton(i,o,e),n=i.click;n&&u.bind(t,"click",function(t){h(n)?r[n]():n(t,u.Editor,i)}),c[i.id]=t},[]),c},n.createButton=function(t,i,n){var o=this.options.i18n,r=t.checkbox,e=t.tabindex,u=t.icon,o=o[t.i18n||t.id];n=[c].concat(u?c+"--icon":null,n);e=p("button",{title:u?o:null,type:"button",tabindex:a(e)?null:e,role:r?"checkbox":null,"aria-checked":r?"false":null,"aria-label":o},i||t.parent);return v(e,n),u?(r=this.options.icons[u])&&y(e,(i=r[0],n=r[1],u=r[2]||"round",r=B("svg",{viewBox:O,class:"ryuseicode__icon"}),i=B("path",{d:i,fill:"currentColor"}),n&&b(i,{"stroke-width":n,"stroke-linecap":u,stroke:"currentColor"}),b(r,{viewBox:O}),r.appendChild(i),r)):function(t,i){if(t){if(a(i))return t.innerHTML;t.innerHTML!==i&&(t.innerHTML=i)}}(e,t.html||o),e},n.createField=function(t,i){var n=this.i18n[t.i18n||t.id],t=t.tabindex;return p("input",{class:"ryuseicode__input",placeholder:n,spellcheck:!1,tabindex:a(t)?null:t,"aria-label":n},i)},n.show=function(t){this.isActive()&&x(this.groups[this.group].elm),v(this.wrapper,u),this.append(t),this.group=t},n.hide=function(){this.isActive()&&(A(this.wrapper,u),x(this.groups[this.group].elm))},n.isActive=function(t){return i=this.wrapper,n=u,i&&i.classList.contains(n)&&(!t||this.group===t);var i,n},n.isFocused=function(){return this.wrapper.contains(document.activeElement)},t}(function(){function t(t){this.Editor=t,this.event=t.event,this.options=t.options,this.language=t.language}var i,n,o=t.prototype;return o.mount=function(t){var n=this;this.elements=t,w(this.Editor.Components,function(t,i){n[i]=t})},o.destroy=function(){!function i(n,t,o){"function"==typeof o?t.split(" ").forEach(function(t){n.removeEventListener(t,o)}):(t=k.get(o))&&(t.forEach(function(t){i(t.elm,t.events,t.callback)}),k.delete(o))}(null,"",this)},o.on=function(t,i,n,o){this.event.on(t,n?i.bind(n):i,this,o)},o.off=function(t){this.event.off(t,this)},o.emit=function(t){for(var i,n=arguments.length,o=new Array(1<n?n-1:0),r=1;r<n;r++)o[r-1]=arguments[r];(i=this.event).emit.apply(i,[t].concat(o))},o.bind=function(t,i,n,o){var r,e,u,c;r=t,e=i,u=o?n.bind(o):n,c=this,e.split(" ").forEach(function(t){r.addEventListener(t,u),c&&((t=k.get(c)||[]).push({elm:r,events:e,callback:u}),k.set(c,t))})},o.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},o.invoke=function(t,i){for(var n,o=arguments.length,r=new Array(2<o?o-2:0),e=2;e<o;e++)r[e-2]=arguments[e];return(n=this.Editor).invoke.apply(n,[t,i].concat(r))},o.require=function(t){return this.Editor.require(t)},o.addIcons=function(t){var i=this.options;i.icons=_({},t,i.icons)},o.addI18n=function(t){var i=this.options;i.i18n=_({},t,i.i18n)},o.addKeyBindings=function(t){var i=this.options;i.keymap=_({},t,i.keymap)},o.getOptions=function(t,i){t=this.options[t];return a(t)||!0===t?i||{}:s(t)?_({},i,t):void D(!1)},i=t,(o=[{key:"lines",get:function(){return this.Code.Lines}},{key:"i18n",get:function(){return this.options.i18n}}])&&r(i.prototype,o),n&&r(i,n),t}()),L={confirm:{id:"confirm",click:"confirm"},cancel:{id:"cancel",click:"hide",tabindex:0}},S=e+"__dialog",T=e+"-common";"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.compose({Dialog:function(i){function t(){return i.apply(this,arguments)||this}o(t,i);var n=t.prototype;return n.mount=function(t){i.prototype.mount.call(this,t),this.register(T,g(),"")},n.listen=function(){var n=this;this.bind(window,"click",function(t){n.wrapper.contains(t.target)||n.hide()}),this.on("initStyle",function(t,i){i("."+S+" code","fontFamily",n.options.monospaceFont)})},n.create=function(){var t=this.elements,i=t.root.id+"-dialog";this.wrapper=g({id:i,class:S,role:"dialog","aria-labelledby":i+"-title","aria-describedby":i+"-body"},t.overlay)},n.confirm=function(){this.emit("dialog:"+this.group+":confirmed",this),this.hide()},n.register=function(t,i,n,o){var r=(o||["confirm"]).map(function(t){return h(t)?L[t]:t}).filter(Boolean);D(r.length);var e=this.wrapper.id,u=g("ryuseicode__dialog__group"),c=p("header","ryuseicode__dialog__header"),s=p("h3",{id:e+"-title",class:"ryuseicode__dialog__title"},c),f=p("footer","ryuseicode__dialog__footer"),o=this.createCloseButton({"aria-controls":e});b(i,{id:e+"-body",class:"ryuseicode__dialog__body"}),j(s,n),v(o,S+"__close"),y(u,[c,i,f,o]),this.groups[t]={elm:u,title:s,body:i,buttons:this.createButtons(r,f,this)}},n.show=function(t){this.hide(),i.prototype.show.call(this,t),this.Editor.readOnly=!0,v(this.elements.overlay,u),this.autoFocus(t),this.emit("dialog:opened",this,t)},n.hide=function(){this.isActive()&&(this.Editor.readOnly=!1,i.prototype.hide.call(this),A(this.elements.overlay,u),this.Selection.reselect(),this.emit("dialog:closed",this,this.group))},n.message=function(t,i){var n=this.groups[T];j(n.title,i||this.i18n.notice),j(n.body,t),this.show(T)},t}(t)})});