UNPKG

@rickx/ckeditor5-line-height

Version:
3 lines (2 loc) 4.23 kB
import{Command as t,first as e,Plugin as i,createDropdown as o,addListToDropdown as n,Collection as s,ViewModel as l}from"ckeditor5";var c='<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1545631834210" class="icon" style="" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1840" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20"><defs><style type="text/css"></style></defs><path d="M648 160H104c-4.4 0-8 3.6-8 8v128c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8v-64h168v560h-92c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h264c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8h-92V232h168v64c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V168c0-4.4-3.6-8-8-8z m272.8 546H856V318h64.8c6 0 9.4-7 5.7-11.7L825.7 178.7c-2.9-3.7-8.5-3.7-11.3 0L713.6 306.3c-3.7 4.7-0.4 11.7 5.7 11.7H784v388h-64.8c-6 0-9.4 7-5.7 11.7l100.8 127.5c2.9 3.7 8.5 3.7 11.3 0l100.8-127.5c3.8-4.7 0.4-11.7-5.6-11.7z" fill="#000000" p-id="1841"></path></svg>';const r="lineHeight";function a(t){return"object"==typeof t&&((e=t).title&&e.model&&e.view)?t:"default"===t?{model:void 0,title:"Default"}:function(t){"object"!=typeof t&&(t={title:String(t),model:String(t)});return{title:t.title,model:t.model}}(t);var e}function d(t){return t.map((t=>a(t))).filter((t=>!!t))}class u extends t{static get pluginName(){return"LineHeightEditing"}constructor(t){super(t)}refresh(){const t=this.editor.model.document,i=e(t.selection.getSelectedBlocks());this.isEnabled=!!i&&this._canSetLineHeight(i),this.value=this.isEnabled&&i.hasAttribute(r)?i.getAttribute(r):"default"}execute(t={}){const e=this.editor.model,i=e.document,o=t.value;e.change((t=>{const e=Array.from(i.selection.getSelectedBlocks()).filter((t=>this._canSetLineHeight(t)));e[0].getAttribute(r)===o||!o?function(t,e){for(const i of t)e.removeAttribute(r,i)}(e,t):function(t,e,i){for(const o of t)e.setAttribute(r,i,o)}(e,t,o)}))}_canSetLineHeight(t){return this.editor.model.schema.checkAttribute(t,r)}}class h extends i{static get pluginName(){return"LineHeightEditing"}constructor(t){super(t),t.config.define(r,{options:["default",1,1.1,1.2,1.3,1.4,1.5,1.6,2,2.5],supportAllValues:!1})}init(){const t=this.editor,e=t.model.schema,i=t.config.get("lineHeight.supportAllValues"),o=d(t.config.get("lineHeight.options")).filter((t=>t.model));e.extend("$block",{allowAttributes:r}),t.model.schema.setAttributeProperties(r,{isFormatting:!0});const n=function(t,e){const i={model:{key:t,values:[]},view:{}};for(const t of e)i.model.values.push(t.model),i.view[t.model]={key:"style",value:{"line-height":t.model}};return i}(r,o);i?this._prepareAnyValueConverters():t.conversion.attributeToAttribute(n),t.commands.add(r,new u(t))}_prepareAnyValueConverters(){const t=this.editor;t.conversion.for("downcast").attributeToAttribute({model:r,view:t=>({key:"style",value:{"line-height":t}})}),t.conversion.for("upcast").elementToAttribute({model:{key:r,value:t=>t.getStyle("line-height")},view:{name:"p",styles:{"line-height":/.*/}}})}}class m extends i{static get pluginName(){return"LineHeightUI"}init(){const t=this.editor,e=t.ui.componentFactory,i=t.t,a=this._getLocalizedOptions(),d=t.commands.get(r);e.add(r,(e=>{const u=o(e);return n(u,function(t,e){const i=new s;for(const o of t){const t={type:"button",model:new l({commandName:r,commandParam:o.model,label:o.title,class:"ck-line-height-option",withText:!0})};t.model.bind("isOn").to(e,"value",(t=>t===o.model)),i.add(t)}return i}(a,d)),u.buttonView.set({label:i("Line Height"),icon:c,tooltip:!0}),u.extendTemplate({attributes:{class:["ck-line-height-dropdown"]}}),u.bind("isEnabled").to(d),this.listenTo(u,"execute",(e=>{t.execute(e.source.commandName,{value:e.source.commandParam}),t.editing.view.focus()})),u}))}_getLocalizedOptions(){const t=this.editor,e={Default:(0,t.t)("Default")};return d(t.config.get(r).options).map((t=>{const i=e[t.title];return i&&i!==t.title&&(t=Object.assign({},t,{title:i})),t}))}}class g extends i{static get requires(){return[h,m]}static get pluginName(){return"LineHeight"}}const p={lineHeight:c};export{r as LINE_HEIGHT,g as LineHeight,h as LineHeightEditing,m as LineHeightUI,p as icons}; //# sourceMappingURL=index.js.map