/** * layout 1.5v 2020-01-14 * The tinymce-plugins is used to set up a layout * * https://github.com/Five-great/tinymce-plugins * * Copyright 2020, Five(Li Hailong) The Chengdu, China https://fivecc.cn/ * * Licensed under MIT */ tinymce.PluginManager.add('layout', function(editor, url) { var pluginName='一键布局'; var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools'); var layout_opt = editor.getParam('layout_options', {clearStyle:[],filterTags:['table>*'],style:{'text-align':'justify','text-indent':'2em','line-height': 1.5},tagsStyle:{}}); var layout_filterTags={}; var layout_filterTagsRegex={}; for( let key in layout_opt.filterTags){layout_opt.filterTags[key].indexOf('>*')!=-1?layout_filterTagsRegex[layout_opt.filterTags[key].replace('>*','').toUpperCase()]=true :layout_filterTags[layout_opt.filterTags[key].toUpperCase()]=true;} for( let key in layout_opt.tagsStyle){ let ckeyList = key.split(','); for(let ckey in ckeyList)ckeyList[ckey].indexOf('>*')!=-1?layout_filterTagsRegex[ckeyList[ckey].replace('>*','').toUpperCase()]=key :layout_filterTags[ckeyList[ckey].toUpperCase()]=key; } var doAct = function () { var dom = editor.dom; editor.execCommand('selectAll'); var blocks = editor.selection.getSelectedBlocks() function _indent2$getValue( key, str ) { var m = str.match( new RegExp(key + ':?(.+?)"?[;}]') ); return m ? m[ 1 ] : false; } function filterFun(el) { let parentSelector = 'BODY'; let parents = el.tagName; if(layout_filterTags[parents] || layout_filterTagsRegex[parents]) { !layout_opt.tagsStyle[layout_filterTags[parents]]?'': setStyleFun(el,layout_opt.tagsStyle[layout_filterTags[parents]]) return true; } let _p = el.parentNode; let _pName = _p.tagName while (_pName !== parentSelector) { let o = _p; parents = _pName + '>' + parents; if(layout_filterTags[parents] || layout_filterTagsRegex[_pName]) { !layout_opt.tagsStyle[layout_filterTagsRegex[_pName]]?'': setStyleFun(el,layout_opt.tagsStyle[layout_filterTagsRegex[_pName]]) !layout_opt.tagsStyle[layout_filterTags[parents]]?'': setStyleFun(el,layout_opt.tagsStyle[layout_filterTags[parents]]) return true; } _p = o.parentNode; _pName = _p.tagName; } return false; } function clearStyleFun(_block){ let style=dom.getAttrib(_block,'style'); for(let key in layout_opt.clearStyle){ let reg = new RegExp(layout_opt.clearStyle[key] + ':?(.+?)"?[;}]') style = style.replace(reg, ''); } dom.setAttrib(_block,'style',style); } function removeStyleFun(_block){ let style=dom.getAttrib(_block,'style'); for(let key in layout_opt.style){ let reg = new RegExp(key + ':?(.+?)"?[;}]') style = style.replace(reg, ''); } dom.setAttrib(_block,'style',style); } function setStyleFun(_block,_style){ for(let key in _style){ dom.setStyle(_block, key, _style[key]); } if(_style["text-indent"]){ let kv = "",kl = ""; if(_block&&_block.children['0']&&_block.children['0'].attributes&&_block.children['0'].attributes.style){ kv = _indent2$getValue('font-size',_block.children['0'].attributes.style.textContent); kl = _indent2$getValue('letter-spacing',_block.children['0'].attributes.style.textContent); if(kv) {kv=(parseInt(kv)+parseInt((kl?kl:0)))*2+'px';} else kv=(parseInt((kl?kl:0))+16)*2+'px'; } dom.setStyle(_block, 'text-indent', layout_opt.style['text-indent']&&layout_opt.style['text-indent']!='2em'?layout_opt.style['text-indent']: kv?kv:'2em'); } } var layoutAct = ''; global$1.each(blocks, function (block) { if(layoutAct==''){if(dom.hasClass(block,'layoutFV')){layoutAct = 'remove'; dom.removeClass(block,'layoutFV')}else{ layoutAct = 'add'; dom.addClass(block,'layoutFV')}} if( layoutAct =='add'){ !filterFun(block)?setStyleFun(block,layout_opt.style):''; layout_opt.clearStyle?clearStyleFun(block):''; }else{ !filterFun(block)?removeStyleFun(block,layout_opt.style):''; } }); }; editor.ui.registry.getAll().icons.layout || editor.ui.registry.addIcon('layout',''); editor.ui.registry.addToggleButton('layout', { icon: 'layout', tooltip: pluginName, onAction: function () { editor.undoManager.transact(function(){ editor.focus(); doAct(); }) } }); editor.ui.registry.addMenuItem('layout', { text: pluginName, onAction: function() { editor.undoManager.transact(function(){ editor.focus(); doAct(); }) } }); return { getMetadata: function () { return { name: pluginName, url: "https://github.com/Five-great/tinymce-plugins", }; } }; });