From 55a2f137cfb3af11d38084257033748f9ef14250 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 31 Aug 2017 16:29:00 +0200 Subject: [PATCH] CKEditor: inline_resize plugin --- js/ckeditor/plugins/inline_resize/plugin.js | 123 ++++++++++++++++++++ js/kivi.js | 18 +-- 2 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 js/ckeditor/plugins/inline_resize/plugin.js diff --git a/js/ckeditor/plugins/inline_resize/plugin.js b/js/ckeditor/plugins/inline_resize/plugin.js new file mode 100644 index 000000000..c6df44af3 --- /dev/null +++ b/js/ckeditor/plugins/inline_resize/plugin.js @@ -0,0 +1,123 @@ +( function() { + var win = CKEDITOR.document.getWindow(), + pixelate = CKEDITOR.tools.cssLength; + + CKEDITOR.plugins.add('inline_resize', { + init: function( editor ) { + var config = editor.config; + + editor.on('loaded', function() { + attach(editor); + }, null, null, 20); // same priority as floatspace so that both get initialized + } + }); + + function attach( editor ) { + var config = editor.config; + + var resize = function (width, height) { + var editable; + if ( !( editable = editor.editable() ) ) + return; + + editable.setStyle('width', pixelate(width)); + editable.setStyle('height', pixelate(height)); + } + + var layout = function ( evt ) { + var editable; + if ( !( editable = editor.editable() ) ) + return; + + // Show up the space on focus gain. + if ( evt && evt.name == 'focus' ) + float_space.show(); + + var editorPos = editable.getDocumentPosition(); + var editorRect = editable.getClientRect(); + var floatRect = float_space.getClientRect(); + var viewRect = win.getViewPaneSize(); + + float_space.setStyle( 'position', 'absolute' ); + float_space.setStyle( 'top', pixelate( editorPos.y + editorRect.height - floatRect.height + 1) ); + float_space.setStyle( 'right', pixelate( viewRect.width - editorRect.right ) ); + }; + + var float_html = '
\u25E2
'; // class so that csss can overrise content and style + var float_space = CKEDITOR.document.getBody().append( CKEDITOR.dom.element.createFromHtml( float_html )); + + var drag_handler = function( evt ) { + var width = startSize.width + evt.data.$.screenX - origin.x, + height = startSize.height + evt.data.$.screenY - origin.y; + + width = Math.max( config.resize_minWidth || 200, Math.min( width || 0, config.resize_maxWidth || 9000) ); + height = Math.max( config.resize_minHeight || 75, Math.min( height || 0, config.resize_maxHeight || 9000) ); + + resize( width, height ); + layout(); + }; + + var drag_end_handler = function() { + CKEDITOR.document.removeListener( 'mousemove', drag_handler ); + CKEDITOR.document.removeListener( 'mouseup', drag_end_handler ); + + if ( editor.document ) { + editor.document.removeListener( 'mousemove', drag_handler ); + editor.document.removeListener( 'mouseup', drag_end_handler ); + } + } + + var mousedown_fn = function( evt ) { + var editable; + if ( !( editable = editor.editable() ) ) + return; + + var editorRect = editable.getClientRect(); + startSize = { width: editorRect.width, height: editorRect.height }; + origin = { x: evt.data.$.screenX, y: evt.data.$.screenY }; + + if (config.resize_minWidth > startSize.width) config.resize_minWidth = startSize.width; + if (config.resize_minHeight > startSize.height) config.resize_minHeight = startSize.height; + + CKEDITOR.document.on( 'mousemove', drag_handler ); + CKEDITOR.document.on( 'mouseup', drag_end_handler ); + + if ( editor.document ) { + editor.document.on( 'mousemove', drag_handler ); + editor.document.on( 'mouseup', drag_end_handler ); + } + + evt.data.$.preventDefault(); + }; + + float_space.setStyle( 'overflow', 'hidden' ); + float_space.setStyle( 'cursor', 'se-resize' ) + float_space.on('mousedown', mousedown_fn); + float_space.unselectable(); + + editor.on( 'focus', function( evt ) { + layout( evt ); + } ); + + editor.on( 'blur', function() { + float_space.hide(); + } ); + + editor.on( 'destroy', function() { + float_space.remove(); + } ); + + if ( editor.focusManager.hasFocus ) + float_space.show(); + + editor.focusManager.add( float_space, 1 ); + } + +})(); + +/* + TODO + * ltr support + * textarea/div mode safe, currently simply assumes that textarea inline is used + * positioning of resize handle is not browser zomm safe +*/ diff --git a/js/kivi.js b/js/kivi.js index fbe0c953b..b4608c9f0 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -222,17 +222,17 @@ namespace("kivi", function(ns) { entities: false, language: 'de', removePlugins: 'resize', - toolbar: buttons - } + extraPlugins: 'inline_resize', + toolbar: buttons, + disableAutoInline: true, + title: false + }; - var style = $e.prop('style'); - $(['width', 'height']).each(function(idx, prop) { - var matches = (style[prop] || '').match(/(\d+)px/); - if (matches && (matches.length > 1)) - config[prop] = matches[1]; - }); + config.height = $e.height(); + config.width = $e.width(); - $e.ckeditor(config); + var editor = CKEDITOR.inline($e.get(0), config); + $e.data('editor', editor); if ($e.hasClass('texteditor-autofocus')) $e.ckeditor(function() { ns.focus_ckeditor($e); }); -- 2.20.1