Skip to content

Commit

Permalink
Merge pull request #736 from ayobami14/setContent
Browse files Browse the repository at this point in the history
Expose setContent method on medium
  • Loading branch information
nmielnik committed Jul 21, 2015
2 parents dbd9af9 + 0f1b6d6 commit de369ca
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
30 changes: 22 additions & 8 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Initialization Functions](#initialization-functions)
- [`MediumEditor(elements, options)`](#mediumeditorelements-options)
- [`destroy()`](#destroy)
Expand Down Expand Up @@ -35,6 +36,7 @@
- [`delay(fn)`](#delayfn)
- [`getExtensionByName(name)`](#getextensionbynamename)
- [`serialize()`](#serialize)
- [`setContent(html, index)`](#setcontenthtml-index)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand All @@ -59,7 +61,7 @@ _**elements** (`String` | `HTMLElement` | `Array`)_:

3. `Array`: If passed as an `Array` of `HTMLElement`s, this will be used as the internal list of **elements**.

_**options** (`Object`)_:
_**options** (`Object`)_:

Set of [custom options](OPTIONS.md) used to initialize `MediumEditor`.

Expand Down Expand Up @@ -91,7 +93,7 @@ Attaches an event listener to specific element via the browser's built-in `addEv

* Element to attach listener to via `addEventListener(type, listener, useCapture)`

2. _**event** (`String`)_:
2. _**event** (`String`)_:

* type argument for `addEventListener(type, listener, useCapture)`

Expand All @@ -114,7 +116,7 @@ Detach an event listener from a specific element via the browser's built-in `rem

* Element to detach listener from via `removeEventListener(type, listener, useCapture)`

2. _**event** (`String`)_:
2. _**event** (`String`)_:

* type argument for `removeEventListener(type, listener, useCapture)`

Expand All @@ -137,7 +139,7 @@ Attaches a listener for the specified custom event name.

* Name of the event to listen to. See the list of built-in [Custom Events](CUSTOM-EVENTS.md).

2. _**listener(data, editable)** (`function`)_:
2. _**listener(data, editable)** (`function`)_:

* Listener method that will be called whenever the custom event is triggered.

Expand All @@ -161,7 +163,7 @@ Detaches a custom event listener for the specified custom event name.

* Name of the event to detach the listener for.

2. _**listener** (`function`)_:
2. _**listener** (`function`)_:

* A reference to the listener to detach. This must be a match by-reference and not a copy.

Expand Down Expand Up @@ -211,7 +213,7 @@ Restores the selection using a data representation of previously selected text (

* Data representing the state of the selection to restore.

2. _**favorLaterSelectionAnchor** (`boolen`)_:
2. _**favorLaterSelectionAnchor** (`boolean`)_:

* If `true`, import the cursor immediately subsequent to an anchor tag if it would otherwise be placed right at the trailing edge inside the anchor. THis cursor positioning, even though visually equivalent to the user, can affect behavior in Internet Explorer.

Expand Down Expand Up @@ -286,7 +288,7 @@ _wrapper around the browser's built in `document.queryCommandState(action)` for
***
## Helper Functions

### `delay(fn)`
### `delay(fn)`

Delay any function from being executed by the amount of time passed as the **delay** option.

Expand All @@ -312,4 +314,16 @@ Get a reference to an extension with the specified name.

Returns a JSON object including the content of each of the **elements** inside the editor.

***
***
### `setContent(html, index)`

Sets the innerHTML content for the element at `index`.
Trigger the `editableInput` event.

**Arguments**

1. _**html** (`string`)_:
* The content to set the element to

2. _**index** (`integer`)_:
* Index of the element to set the content on. Defaults to 0 when not provided.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ View the [MediumEditor Object API documentation](API.md) on the Wiki for details
* __.delay(fn)__: delay any function from being executed by the amount of time passed as the `delay` option
* __.getExtensionByName(name)__: get a reference to an extension with the specified name
* __.serialize()__: returns a JSON object with elements contents
* __.setContent(html, element)__: sets the `innerHTML` to `html` of the element at `index`

## Capturing DOM changes

Expand Down
19 changes: 18 additions & 1 deletion spec/core-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ describe('Core-API', function () {
expect(focused).toBe(elementOne);
});
});
});

describe('setContent', function () {
it('should set the content of the editor\'s element', function () {
var newHTML = 'Lorem ipsum dolor',
otherHTML = 'something different',
elementOne = this.createElement('div', 'editor', 'lorem ipsum'),
editor = this.newMediumEditor('.editor');

editor.setContent(newHTML);
expect(this.el.innerHTML).toEqual(newHTML);
expect(elementOne.innerHTML).not.toEqual(newHTML);

editor.setContent(otherHTML, 1);
expect(elementOne.innerHTML).toEqual(otherHTML);
expect(this.el.innerHTML).not.toEqual(otherHTML);
});
});
});
33 changes: 33 additions & 0 deletions spec/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,39 @@ describe('Events TestCase', function () {

Events.prototype.InputEventOnContenteditableSupported = originalInputSupport;
});

it(namePrefix + ',setContent should fire editableInput when content changes', function () {
var newHTML = 'Lorem ipsum dolor',
editor = this.newMediumEditor('.editor'),
spy = jasmine.createSpy('handler'),
originalInputSupport = Events.prototype.InputEventOnContenteditableSupported;

Events.prototype.InputEventOnContenteditableSupported = inputSupported;

editor.subscribe('editableInput', spy);
expect(spy).not.toHaveBeenCalled();

editor.setContent(newHTML, 0);
var obj = { target: this.el, currentTarget: this.el };
expect(spy).toHaveBeenCalledWith(obj, this.el);
Events.prototype.InputEventOnContenteditableSupported = originalInputSupport;
});

it(namePrefix + ', setContent should not fire editableInput when content doesn\'t change', function () {
var sameHTML = 'lore ipsum',
editor = this.newMediumEditor('.editor'),
spy = jasmine.createSpy('handler'),
originalInputSupport = Events.prototype.InputEventOnContenteditableSupported;

Events.prototype.InputEventOnContenteditableSupported = inputSupported;

editor.subscribe('editableInput', spy);
expect(spy).not.toHaveBeenCalled();

editor.setContent(sameHTML, 0);
expect(spy).not.toHaveBeenCalled();
Events.prototype.InputEventOnContenteditableSupported = originalInputSupport;
});
}

runEditableInputTests(true);
Expand Down
10 changes: 10 additions & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,16 @@ function MediumEditor(elements, options) {

pasteHTML: function (html, options) {
this.getExtensionByName('paste').pasteHTML(html, options);
},

setContent: function (html, index) {
index = index || 0;

if (this.elements[index]) {
var target = this.elements[index];
target.innerHTML = html;
this.events.updateInput(target, { target: target, currentTarget: target });
}
}
};
}());
3 changes: 3 additions & 0 deletions src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ var Events;
},

updateInput: function (target, eventObj) {
if (!this.contentCache) {
return;
}
// An event triggered which signifies that the user may have changed someting
// Look in our cache of input for the contenteditables to see if something changed
var index = target.getAttribute('medium-editor-index');
Expand Down

0 comments on commit de369ca

Please sign in to comment.