Skip to content

Commit 77641cd

Browse files
authored
Merge pull request #889 from crazyserver/MOBILE-1706
MOBILE-1706 rte: Auto adjust height on RTE
2 parents ae433c2 + ab80ab4 commit 77641cd

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

upgrade.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ information provided here is intended especially for developers.
1414
ionic plugin add cordova-universal-clipboard
1515
or
1616
ionic state restore
17+
* mm-rich-text-editor now admits a new parameter adjust-height (default: true). It admits an HTML DOM Id string or boolean. Specifying it will adjust height of the editor to the selected element. If true it will adjust to the whole page and false will use height parameter instead.
1718

1819
=== 3.2 ===
1920

www/core/directives/richtexteditor.js

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ angular.module('mm.core')
5252
.directive('mmRichTextEditor', function($ionicPlatform, $mmLang, $timeout, $q, $window, $ionicScrollDelegate, $mmUtil,
5353
$mmSite, $mmFilepool) {
5454

55-
var editorInitialHeight = 300,
55+
var editorInitialHeightDefault = 300,
56+
adjustHeightDefault = true,
5657
frameTags = ['iframe', 'frame', 'object', 'embed'];
5758

5859
/**
@@ -251,10 +252,13 @@ angular.module('mm.core')
251252
element = element[0];
252253

253254
// More customization in http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-customConfig
255+
// Option adjustHeight can be boolean or HTML DOM id. Specifying it will adjust height of the editor to the selected
256+
// element.
254257
var defaultOptions = {
255258
allowedContent: true,
256259
defaultLanguage: 'en',
257-
height: editorInitialHeight,
260+
height: editorInitialHeightDefault,
261+
adjustHeight: adjustHeightDefault,
258262
toolbarCanCollapse: true,
259263
toolbarStartupExpanded: false,
260264
toolbar: [
@@ -276,7 +280,9 @@ angular.module('mm.core')
276280
component = scope.component,
277281
componentId = scope.componentId,
278282
firstChange = true,
279-
renderTime;
283+
renderTime,
284+
editorInitialHeight = editorInitialHeightDefault,
285+
adjustHeight = adjustHeightDefault;
280286

281287
// Default to text.
282288
scope.property = typeof scope.property == 'string' ? scope.property : 'text';
@@ -300,6 +306,9 @@ angular.module('mm.core')
300306
} else {
301307
scope.editorOptions = angular.extend(defaultOptions, scope.options, scope.phoneOptions);
302308
}
309+
310+
editorInitialHeight = scope.editorOptions.height;
311+
adjustHeight = scope.editorOptions.adjustHeight;
303312
});
304313
}
305314
});
@@ -359,11 +368,14 @@ angular.module('mm.core')
359368
}
360369

361370
// If text isn't changed we won't throw firstRender, so throw it manually.
362-
if (scope.richTextEditor && scope.firstRender) {
371+
if (scope.richTextEditor) {
363372
$timeout(function() {
364373
if (firstChange) {
365-
scope.firstRender();
374+
if (scope.firstRender) {
375+
scope.firstRender();
376+
}
366377
firstChange = false;
378+
resizeContent(editorEl, contentsEl, toolbar);
367379
}
368380
}, 1000);
369381
}
@@ -418,7 +430,7 @@ angular.module('mm.core')
418430
// Resize the editor to fill the visible screen size (except top bar).
419431
function resizeContent(editorEl, contentsEl, toolbar) {
420432
var toolbarHeight = toolbar.offsetHeight || toolbar.height || toolbar.clientHeight || 0,
421-
editorHeightWithoutResize = editorInitialHeight + toolbarHeight,
433+
editorWithToolbarHeight = editorInitialHeight + toolbarHeight,
422434
contentVisibleHeight,
423435
editorContentNewHeight,
424436
screenSmallerThanEditor,
@@ -430,16 +442,50 @@ angular.module('mm.core')
430442

431443
editorMaximized = !!editorEl.querySelector('.cke_maximized');
432444
contentVisibleHeight = $window.innerHeight - fixedBarsHeight;
433-
screenSmallerThanEditor = !editorMaximized && contentVisibleHeight > 0 && contentVisibleHeight < editorHeightWithoutResize;
434-
editorContentNewHeight = contentVisibleHeight - toolbarHeight;
445+
446+
// Editor is ready, adjust Height if needed.
447+
if (adjustHeight && !editorMaximized && contentVisibleHeight > 0) {
448+
var currentElement = element,
449+
topElement,
450+
height = 0;
451+
452+
if (adjustHeight !== true) {
453+
topElement = document.getElementById(adjustHeight);
454+
contentVisibleHeight = topElement.offsetHeight || topElement.height || topElement.clientHeight ||
455+
contentVisibleHeight;
456+
}
457+
while (currentElement.parentNode && currentElement.parentNode.tagName != "ION-CONTENT" &&
458+
(!topElement || currentElement != topElement)) {
459+
460+
var parent = currentElement.parentNode;
461+
angular.forEach(parent.childNodes, function(child) {
462+
if (child.tagName && currentElement.tagName &&
463+
currentElement.tagName != 'MM-LOADING' && child != currentElement) {
464+
height += (child.offsetHeight || child.height || child.clientHeight);
465+
var cs = getComputedStyle(child);
466+
height += (parseInt(cs.borderTop, 10) + parseInt(cs.borderBottom, 10));
467+
}
468+
});
469+
currentElement = parent;
470+
}
471+
var cs = getComputedStyle(currentElement);
472+
height += (parseInt(cs.paddingTop, 10) + parseInt(cs.paddingBottom, 10));
473+
if (contentVisibleHeight > height) {
474+
editorWithToolbarHeight = contentVisibleHeight - height;
475+
editorInitialHeight = editorWithToolbarHeight - toolbarHeight;
476+
}
477+
}
478+
screenSmallerThanEditor = !editorMaximized && contentVisibleHeight > 0 &&
479+
contentVisibleHeight < editorWithToolbarHeight;
480+
editorContentNewHeight = editorWithToolbarHeight - toolbarHeight;
435481

436482
if (resized && !screenSmallerThanEditor) {
437483
// The editor was resized but now it isn't needed anymore, undo the resize.
438484
undoResize(editorEl, contentsEl);
439-
} else if (editorContentNewHeight > 50 && (resized || screenSmallerThanEditor)) {
485+
} else if (editorContentNewHeight > 60 && (resized || screenSmallerThanEditor || adjustHeight)) {
440486
// The visible screen size is lower than the editor size, resize editor to fill the screen.
441487
// We don't resize if the content new height is too small.
442-
angular.element(editorEl).css('height', contentVisibleHeight + 'px');
488+
angular.element(editorEl).css('height', editorWithToolbarHeight + 'px');
443489
angular.element(contentsEl).css('height', editorContentNewHeight + 'px');
444490
resized = true;
445491

www/core/scss/styles.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ mm-timer {
897897
}
898898

899899
.item.mm-item-has-rich-text-editor {
900+
&.item-input {
901+
padding: 0;
902+
}
900903
padding: 0;
901904
display: block;
902905

0 commit comments

Comments
 (0)