-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable event manager when editing is disabled
Previously, manual (as opposed to programmatic) editing was being prevented for an editor after calling `#disableEditing` by setting "contenteditable" back to false for the editor. This prevents the browser from sending most of the edit-related events (keyup, keydown, etc) that occur when editing manually. But some events (like paste) are still emitted by the browser even though "contenteditable" is false. This change calls `EventManager#stop` when editing is disabled, and internally the event manager ignores all events when it is stopped. Also some rearrangement Other changes * Add test for triggering paste event in a disabled editor * add assertion `assert.isBlank` * better assertion actual/expected values for `hasElement` and `hasNoElement` * add test helper `getData` (for parity with the `setData` in element-utils and to avoid using jQuery's `.data()` method) * consolidate the editing-disabled tests into a new suite * make `editor#render` call `disableEditing` or `enableEditing` after it sets `hasRendered` to ensure that the event manager is stopped if the user calls `editor.disableEditing()` before calling `editor.render()` Fixes #572
- Loading branch information
Showing
6 changed files
with
129 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Editor } from 'mobiledoc-kit'; | ||
import Helpers from '../test-helpers'; | ||
import { TAB, ENTER } from 'mobiledoc-kit/utils/characters'; | ||
import { MIME_TEXT_PLAIN } from 'mobiledoc-kit/utils/parse-utils'; | ||
|
||
const { test, module } = Helpers; | ||
|
||
const cards = [{ | ||
name: 'my-card', | ||
type: 'dom', | ||
render() {}, | ||
edit() {} | ||
}]; | ||
|
||
let editor, editorElement; | ||
|
||
module('Acceptance: editor: #disableEditing', { | ||
beforeEach() { | ||
editorElement = $('#editor')[0]; | ||
}, | ||
afterEach() { | ||
if (editor) { editor.destroy(); } | ||
} | ||
}); | ||
|
||
test('#disableEditing before render is meaningful', (assert) => { | ||
editor = new Editor(); | ||
editor.disableEditing(); | ||
editor.render(editorElement); | ||
|
||
assert.equal(editorElement.getAttribute('contenteditable'),'false', | ||
'element is not contenteditable'); | ||
editor.enableEditing(); | ||
assert.equal(editorElement.getAttribute('contenteditable'), 'true', | ||
'element is contenteditable'); | ||
}); | ||
|
||
test('when editing is disabled, the placeholder is not shown', (assert) => { | ||
editor = new Editor({placeholder: 'the placeholder'}); | ||
editor.disableEditing(); | ||
editor.render(editorElement); | ||
|
||
assert.isBlank(Helpers.dom.getData(editorElement, 'placeholder'), | ||
'no placeholder when disabled'); | ||
editor.enableEditing(); | ||
assert.equal(Helpers.dom.getData(editorElement, 'placeholder'), 'the placeholder', | ||
'placeholder is shown when editable'); | ||
}); | ||
|
||
test('#disableEditing and #enableEditing toggle contenteditable', (assert) => { | ||
editor = new Editor(); | ||
editor.render(editorElement); | ||
|
||
assert.equal(editorElement.getAttribute('contenteditable'), | ||
'true', | ||
'element is contenteditable'); | ||
editor.disableEditing(); | ||
assert.equal(editorElement.getAttribute('contenteditable'), | ||
'false', | ||
'element is not contenteditable'); | ||
editor.enableEditing(); | ||
assert.equal(editorElement.getAttribute('contenteditable'), | ||
'true', | ||
'element is contenteditable'); | ||
}); | ||
|
||
// https://github.com/bustle/mobiledoc-kit/issues/572 | ||
test('pasting after #disableEditing does not insert text', function(assert) { | ||
editor = Helpers.editor.buildFromText('abc|', {element: editorElement}); | ||
|
||
Helpers.dom.setCopyData(MIME_TEXT_PLAIN, 'def'); | ||
Helpers.dom.triggerPasteEvent(editor); | ||
assert.hasElement('#editor:contains(abcdef)', 'precond - text is pasted'); | ||
|
||
editor.disableEditing(); | ||
|
||
Helpers.dom.selectText(editor, 'def'); | ||
Helpers.dom.setCopyData(MIME_TEXT_PLAIN, 'ghi'); | ||
Helpers.dom.triggerPasteEvent(editor); | ||
assert.hasNoElement('#editor:contains(ghi)', 'text is not pasted after #disableEditing'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters