From 7e15692cd46b5a21d46228b16fd6a0d1908e6b93 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Wed, 2 Mar 2016 15:06:21 +0300 Subject: [PATCH 1/2] Added text-to-h1 transformation using new cEditor.html module --- codex-editor.js | 72 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/codex-editor.js b/codex-editor.js index dfacbfb6..731e03b5 100644 --- a/codex-editor.js +++ b/codex-editor.js @@ -214,17 +214,18 @@ cEditor.callback = { 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; + case cEditor.core.keys.UP : /** | */ + case cEditor.core.keys.DOWN : /** | */ + case cEditor.core.keys.LEFT : /** V */ + case cEditor.core.keys.RIGHT : this.arrowKeyPressed(event); break; } - - /** temporary */ - event.preventDefault(); - - }, tabKeyPressed : function(event){ + event.preventDefault(); + console.log('TAB pressed: %o', event); if ( !cEditor.toolbar.opened ) { @@ -237,18 +238,33 @@ cEditor.callback = { enterKeyPressed : function(event){ - console.log('ENTER pressed'); - cEditor.toolbar.close(); + console.log('Enter pressed'); + + if (cEditor.toolbar.opened && event.target == cEditor.nodes.redactor) { + + event.preventDefault(); + cEditor.toolbar.toolClicked(); + cEditor.toolbar.close(); + + }; }, escapeKeyPressed : function(event){ + event.preventDefault(); console.log('Escape pressed'); cEditor.toolbar.close(); - } + }, + arrowKeyPressed : function(event){ + + console.log('Arrow pressed'); + if (event.target == cEditor.nodes.redactor) { + console.log(cEditor.html.getNodeFocused()); + }; + } }; @@ -319,13 +335,51 @@ cEditor.toolbar = { this.current = toolToSelect; + }, + + /** + * Transforming selected node type into selected toolbar element type + */ + toolClicked : function() { + + var nodeFocused = cEditor.html.getNodeFocused(); + + switch (cEditor.toolbar.current) { + case 'header' : newTag = 'h1'; + }; + + cEditor.html.switchNode(nodeFocused, newTag); } - }; +cEditor.html = { + getNodeFocused : function() { + + var selection = window.getSelection(); + + if (selection.anchorNode != null) { + return selection.anchorNode.tagName ? selection.anchorNode : selection.focusNode.parentElement; + } else { + return null; + } + + }, + + switchNode : function (targetNode, tagName) { + + /** */ + if (!targetNode && !tagName) return; + + var newNode = cEditor.draw.block(tagName, targetNode.innerHTML); + + cEditor.nodes.redactor.replaceChild(newNode, targetNode); + + } + +}; /** * Content parsing module From 87a53a7086b12909708e7e076df78c3895a7e1f9 Mon Sep 17 00:00:00 2001 From: Andrew S Date: Tue, 8 Mar 2016 23:07:11 +0300 Subject: [PATCH 2/2] Added toolbar movement --- codex-editor.js | 38 ++++++++++++++++++++++++++++---------- editor.css | 8 ++++++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/codex-editor.js b/codex-editor.js index 731e03b5..25513842 100644 --- a/codex-editor.js +++ b/codex-editor.js @@ -198,7 +198,7 @@ cEditor.ui = { cEditor.core.log('ui.bindEvents fired', 'info'); /** All keydowns on Document */ - document.addEventListener('keydown', function (event) { + document.addEventListener('keyup', function (event) { cEditor.callback.globalKeydown(event); }, false ); @@ -214,10 +214,8 @@ cEditor.callback = { 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; - case cEditor.core.keys.UP : /** | */ - case cEditor.core.keys.DOWN : /** | */ - case cEditor.core.keys.LEFT : /** V */ - case cEditor.core.keys.RIGHT : this.arrowKeyPressed(event); break; + case cEditor.core.keys.UP : + case cEditor.core.keys.DOWN : this.arrowKeyPressed(event); break; } }, @@ -261,16 +259,20 @@ cEditor.callback = { arrowKeyPressed : function(event){ console.log('Arrow pressed'); - if (event.target == cEditor.nodes.redactor) { - console.log(cEditor.html.getNodeFocused()); - }; + + cEditor.toolbar.close(); + + cEditor.toolbar.move(cEditor.html.getNodeFocused()); + } }; cEditor.toolbar = { - opened : false, + defaultOffset : 30, + + opened : false, current : null, @@ -342,7 +344,8 @@ cEditor.toolbar = { */ toolClicked : function() { - var nodeFocused = cEditor.html.getNodeFocused(); + var nodeFocused = cEditor.html.getNodeFocused(), + newTag; switch (cEditor.toolbar.current) { case 'header' : newTag = 'h1'; @@ -350,6 +353,21 @@ cEditor.toolbar = { cEditor.html.switchNode(nodeFocused, newTag); + }, + + + /** + * Moving toolbar to the specified node + */ + move : function(destinationBlock) { + + console.log(cEditor.nodes.toolbar); + + var newYCoordinate = destinationBlock.offsetTop - cEditor.toolbar.defaultOffset - + cEditor.nodes.toolbar.clientHeight; + + cEditor.nodes.toolbar.style.transform = "translateY(" + newYCoordinate + "px)"; + } }; diff --git a/editor.css b/editor.css index 89cde5fc..64224718 100644 --- a/editor.css +++ b/editor.css @@ -70,6 +70,11 @@ /* EDITOR */ + +.ce_wrapper { + position: relative; +} + .ce_redactor { position: relative; outline: none; @@ -90,9 +95,8 @@ box-shadow: 0 2px 11px rgba(27,39,54,.11); color: #2e394b; - top: 300px; - display: none; + } .ce_toolbar.opened{ display: block;