Skip to content

Commit

Permalink
Implements elementsContainer, solves #214
Browse files Browse the repository at this point in the history
  • Loading branch information
daviferreira committed May 1, 2014
1 parent 092f525 commit d24db59
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ If you want to support IE9, you will need to use a classList pollyfill, like Eli
* __anchorPreviewHideDelay__: time in milliseconds to show the anchor tag preview after the mouse has left the anchor tag. Default: 500
* __buttons__: the set of buttons to display on the toolbar. Default: ['bold', 'italic', 'underline', 'anchor', 'header1', 'header2', 'quote']
* __buttonLabels__: type of labels on the buttons. Values: 'fontawesome', `{'bold': '<b>b</b>', 'italic': '<i>i</i>'}`. Default: false
* __checkLinkFormat__: enables/disables check for http on anchor links. Default:
false
* __checkLinkFormat__: enables/disables check for http on anchor links. Default: false
* __cleanPastedHTML__: cleans pasted content from different sources, like google docs etc. Default: false
* __delay__: time in milliseconds to show the toolbar or anchor tag preview. Default: 0
* __diffLeft__: value in pixels to be added to the X axis positioning of the toolbar. Default: 0
Expand All @@ -64,6 +63,7 @@ If you want to support IE9, you will need to use a classList pollyfill, like Eli
* __disableDoubleReturn__: allows/disallows two (or more) empty new lines. You can also set specific element behavior by using setting a data-disable-double-return attribute. Default: false
* __disableToolbar__: enables/disables the toolbar, adding only the contenteditable behavior. You can also set specific element behavior by using setting a data-disable-toolbar attribute. Default: false
* __disableEditing__: enables/disables adding the contenteditable behavior. Useful for using the toolbar with customized buttons/actions. You can also set specific element behavior by using setting a data-disable-editing attribute. Default: false
* __elementsContainer__: specifies a DOM node to contain MediumEditor's toolbar and anchor preview elements. Default: document.body
* __firstHeader__: HTML tag to be used as first header. Default: h3
* __forcePlainText__: Forces pasting as plain text. Default: true
* __placeholder__: Defines the default placeholder for empty contenteditables. You can overwrite it by setting a data-placeholder attribute on your elements. Default: 'Type your text'
Expand Down
12 changes: 8 additions & 4 deletions dist/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ if (typeof module === 'object') {
disableDoubleReturn: false,
disableToolbar: false,
disableEditing: false,
elementsContainer: false,
firstHeader: 'h3',
forcePlainText: true,
placeholder: 'Type your text',
Expand Down Expand Up @@ -157,6 +158,9 @@ if (typeof module === 'object') {
}
// Init toolbar
if (addToolbar) {
if (!this.options.elementsContainer) {
this.options.elementsContainer = document.body;
}
this.initToolbar()
.bindButtons()
.bindAnchorForm()
Expand Down Expand Up @@ -382,7 +386,7 @@ if (typeof module === 'object') {
toolbar.className = 'medium-editor-toolbar';
toolbar.appendChild(this.toolbarButtons());
toolbar.appendChild(this.toolbarFormAnchor());
document.body.appendChild(toolbar);
this.options.elementsContainer.appendChild(toolbar);
return toolbar;
},

Expand Down Expand Up @@ -881,7 +885,7 @@ if (typeof module === 'object') {
anchorPreview.id = 'medium-editor-anchor-preview-' + this.id;
anchorPreview.className = 'medium-editor-anchor-preview';
anchorPreview.innerHTML = this.anchorPreviewTemplate();
document.body.appendChild(anchorPreview);
this.options.elementsContainer.appendChild(anchorPreview);

anchorPreview.addEventListener('click', function () {
self.anchorPreviewClickHandler();
Expand Down Expand Up @@ -1032,8 +1036,8 @@ if (typeof module === 'object') {
this.isActive = false;

if (this.toolbar !== undefined) {
document.body.removeChild(this.anchorPreview);
document.body.removeChild(this.toolbar);
this.options.elementsContainer.removeChild(this.anchorPreview);
this.options.elementsContainer.removeChild(this.toolbar);
delete this.toolbar;
delete this.anchorPreview;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/js/medium-editor.min.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions spec/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('Initialization TestCase', function () {
disableDoubleReturn: false,
disableEditing: false,
disableToolbar: false,
elementsContainer: document.body,
firstHeader: 'h3',
forcePlainText: true,
cleanPastedHTML: false,
Expand Down Expand Up @@ -134,5 +135,26 @@ describe('Initialization TestCase', function () {
expect(editor2.id).toBe(2);
expect(editor3.id).toBe(3);
});

it('should use document.body as element container when no container element is specified', function () {
spyOn(document.body, 'appendChild').and.callThrough();
(function () {
return new MediumEditor('.editor');
}());
expect(document.body.appendChild).toHaveBeenCalled();
});

it('should accept a custom element container for MediumEditor elements', function () {
var div = document.createElement('div');
document.body.appendChild(div);
spyOn(div, 'appendChild').and.callThrough();
(function () {
return new MediumEditor('.editor', {
elementsContainer: div
});
}());
expect(div.appendChild).toHaveBeenCalled();
document.body.removeChild(div);
});
});
});
12 changes: 8 additions & 4 deletions src/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ if (typeof module === 'object') {
disableDoubleReturn: false,
disableToolbar: false,
disableEditing: false,
elementsContainer: false,
firstHeader: 'h3',
forcePlainText: true,
placeholder: 'Type your text',
Expand Down Expand Up @@ -159,6 +160,9 @@ if (typeof module === 'object') {
}
// Init toolbar
if (addToolbar) {
if (!this.options.elementsContainer) {
this.options.elementsContainer = document.body;
}
this.initToolbar()
.bindButtons()
.bindAnchorForm()
Expand Down Expand Up @@ -384,7 +388,7 @@ if (typeof module === 'object') {
toolbar.className = 'medium-editor-toolbar';
toolbar.appendChild(this.toolbarButtons());
toolbar.appendChild(this.toolbarFormAnchor());
document.body.appendChild(toolbar);
this.options.elementsContainer.appendChild(toolbar);
return toolbar;
},

Expand Down Expand Up @@ -883,7 +887,7 @@ if (typeof module === 'object') {
anchorPreview.id = 'medium-editor-anchor-preview-' + this.id;
anchorPreview.className = 'medium-editor-anchor-preview';
anchorPreview.innerHTML = this.anchorPreviewTemplate();
document.body.appendChild(anchorPreview);
this.options.elementsContainer.appendChild(anchorPreview);

anchorPreview.addEventListener('click', function () {
self.anchorPreviewClickHandler();
Expand Down Expand Up @@ -1034,8 +1038,8 @@ if (typeof module === 'object') {
this.isActive = false;

if (this.toolbar !== undefined) {
document.body.removeChild(this.anchorPreview);
document.body.removeChild(this.toolbar);
this.options.elementsContainer.removeChild(this.anchorPreview);
this.options.elementsContainer.removeChild(this.toolbar);
delete this.toolbar;
delete this.anchorPreview;
}
Expand Down

0 comments on commit d24db59

Please sign in to comment.