/** * CodeX Editor * https://ifmo.su/editor * @author CodeX team team@ifmo.su */ var cEditor = (function (cEditor) { // Default settings cEditor.settings = { tools : ['header', 'picture', 'list', 'quote', 'code', 'twitter', 'instagram', 'smile'], textareaId : 'codex-editor', // First-level tags viewing as separated blocks. Other'll be inserted as child blockTags : ['P','BLOCKQUOTE','UL','CODE','OL','H1','H2','H3','H4','H5','H6'] }; // Static nodes cEditor.nodes = { textarea : null, wrapper : null, toolbar : null, toolbarButtons : {}, // { type : DomEl, ... } redactor : null } // Current editor state cEditor.state = { html : '', blocks : [] } /** * Initialization * @uses Promise cEditor.core.prepare * @param {} userSettings are : * - tools [], * - textareaId String * ... */ cEditor.start = function (userSettings) { // Prepare editor settings this.core.prepare(userSettings) // If all ok, make UI, bind events and parse initial-content .then(this.ui.make) .then(this.ui.bindEvents) .then(this.parser.parseTextareaContent) .catch(function (error) { cEditor.core.log('Initialization failed with error: %o', 'warn', error); }) }; return cEditor; })({}); /** * Redactor core methods * Methods: * - init * - log * - insertAfter * - isDomNode */ cEditor.core = { /** * Editor preparing method * @return Promise */ prepare : function (userSettings) { return new Promise(function(resolve, reject){ if ( userSettings ) { cEditor.settings.tools = userSettings.tools || cEditor.settings.tools; } cEditor.nodes.textarea = document.getElementById(userSettings.textareaId || cEditor.settings.textareaId); if (typeof cEditor.nodes.textarea == undefined || cEditor.nodes.textarea == null) { reject(Error("Textarea wasn't found by ID: #" + userSettings.textareaId)); } else { resolve(); } }); }, /** * Logging method * @param type = ['log', 'info', 'warn'] */ log : function (msg, type, arg) { type = type || 'log'; if (!arg) { arg = msg || 'undefined'; msg = '[codex-editor]: %o'; } else { msg = '[codex-editor]: ' + msg; } try{ if ( 'console' in window && console[ type ] ){ if ( arg ) console[ type ]( msg , arg ); else console[ type ]( msg ); } }catch(e){} }, /** * Helper for insert one element after another */ insertAfter : function (target, element) { target.parentNode.insertBefore(element, target.nextSibling); }, /** * Readable DOM-node types map */ nodeTypes : { TAG : 1, TEXT : 3, COMMENT : 8 }, /** * Readable keys map */ keys : { TAB: 9, ENTER: 13, BACKSPACE: 8, DELETE: 46, SPACE: 32, ESC: 27, CTRL: 17, META: 91, SHIFT: 16, ALT: 18, LEFT: 37, UP: 38, DOWN: 40, RIGHT: 39 }, /** * Check object for DOM node */ isDomNode : function (el) { return el && typeof el === 'object' && el.nodeType && el.nodeType == this.nodeTypes.TAG; } } cEditor.ui = { /** * Making main interface */ make : function () { var wrapper, toolbar, tool, redactor; /** Make editor wrapper */ wrapper = cEditor.draw.wrapper(); /** Append editor wrapper after initial textarea */ cEditor.core.insertAfter(cEditor.nodes.textarea, wrapper); /** Make toolbar and content-editable redactor */ toolbar = cEditor.draw.toolbar(); redactor = cEditor.draw.redactor(); wrapper.appendChild(toolbar); wrapper.appendChild(redactor); /** Make toolbar buttons */ cEditor.settings.tools.forEach(function(type) { tool = cEditor.draw.toolbarButton(type); toolbar.appendChild(tool); /** Save tools to static nodes */ cEditor.nodes.toolbarButtons[type] = tool; }); /** Save created ui-elements to static nodes state */ cEditor.nodes.wrapper = wrapper; cEditor.nodes.toolbar = toolbar; cEditor.nodes.redactor = redactor; }, /** * Bind editor UI events */ bindEvents : function () { cEditor.core.log('ui.bindEvents fired', 'info'); /** All keydowns on Document */ document.addEventListener('keydown', function (event) { cEditor.callback.globalKeydown(event); }, false ); /** All keydowns on Document */ document.addEventListener('keyup', function (event) { cEditor.callback.globalKeyup(event); }, false ); /** Mouse click to radactor */ cEditor.nodes.redactor.addEventListener('click', function (event) { cEditor.callback.redactorClicked(event); }, false ); /** Any redactor changes: keyboard input, mouse cut/paste, drag-n-drop text */ cEditor.nodes.redactor.addEventListener('input', function (event) { cEditor.callback.redactorInputEvent(event); }, false ); // for (button in cEditor.nodes.toolbarButtons){ // cEditor.nodes.toolbarButtons[button].addEventListener('click', cEditor.toolbar. toolClicked, false); // } } }; cEditor.callback = { redactorSyncTimeout : null, globalKeydown : function(event){ switch (event.keyCode){ case cEditor.core.keys.TAB : this.tabKeyPressed(event); break; case cEditor.core.keys.ENTER : this.enterKeyPressed(event); break; case cEditor.core.keys.ESC : this.escapeKeyPressed(event); break; } }, globalKeyup : function(event){ switch (event.keyCode){ case cEditor.core.keys.UP : case cEditor.core.keys.DOWN : this.arrowKeyPressed(event); break; } }, tabKeyPressed : function(event){ if ( !cEditor.toolbar.opened ) { cEditor.toolbar.open(); } else { cEditor.toolbar.leaf(); } event.preventDefault(); }, enterKeyPressed : function(event){ cEditor.content.workingNodeChanged(); if (cEditor.toolbar.opened && event.target == cEditor.nodes.redactor) { event.preventDefault(); cEditor.toolbar.toolClicked(event); // cEditor.toolbar.close(); }; }, escapeKeyPressed : function(event){ cEditor.toolbar.close(); event.preventDefault(); }, arrowKeyPressed : function(event){ cEditor.content.workingNodeChanged(); // cEditor.toolbar.close(); cEditor.toolbar.move(); }, redactorClicked : function (event) { cEditor.content.workingNodeChanged(); cEditor.toolbar.move(); cEditor.toolbar.open(); }, redactorInputEvent : function (event) { /** * Clear previous sync-timeout */ if (this.redactorSyncTimeout){ clearTimeout(this.redactorSyncTimeout); } /** * Start waiting to input finish and sync redactor */ this.redactorSyncTimeout = setTimeout(function() { cEditor.content.sync(); }, 500); } }; cEditor.content = { currentNode : null, /** * Synchronizes redactor with original textarea */ sync : function () { cEditor.core.log('syncing...'); /** * Save redactor content to cEditor.state */ cEditor.state.html = cEditor.nodes.redactor.innerHTML; /** * Put it to the textarea */ cEditor.nodes.textarea.value = cEditor.state.html; }, getNodeFocused : function() { var selection = window.getSelection(), focused; if (selection.anchorNode != null) { if ( selection.anchorNode.nodeType == cEditor.core.nodeTypes.TAG ) { focused = selection.anchorNode; } else { focused = selection.focusNode.parentElement; } } if ( !cEditor.parser.isFirstLevelBlock(focused) ) { focused = focused.parentElement; } console.log('focused' , focused); if (focused != cEditor.nodes.redactor){ return focused; } return null; }, /** * Trigger this event when working node changed */ workingNodeChanged : function (setCurrent) { this.currentNode = setCurrent || this.getNodeFocused(); }, switchBlock : function (targetBlock, newBlockTagname) { if (!targetBlock || !newBlockTagname) return; var nodeToReplace; /** * First-level nodes replaces as-is, * otherwise we need to replace parent node */ if (cEditor.parser.isFirstLevelBlock(targetBlock)) { nodeToReplace = targetBlock; } else { nodeToReplace = targetBlock.parentNode; } /** * Make new node with original content */ var nodeCreated = cEditor.draw.block(newBlockTagname, targetBlock.innerHTML); /** * If it is a first-level node, replace as-is. */ if (cEditor.parser.isFirstLevelBlock(nodeCreated)) { cEditor.nodes.redactor.replaceChild(nodeCreated, nodeToReplace); /** * Set new node as current */ cEditor.content.workingNodeChanged(nodeCreated); setTimeout(function() { cEditor.content.currentNode.focus(); }, 100); return; } /** * If it is not a first-level node, for example LI or IMG * we need to wrap it in block-tag (

or