Skip to content
Snippets Groups Projects
Commit 0a3fb72a authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

[misc] Improve typing simulation used for debugging / testing realtime edit during development.

parent c19f00a9
No related branches found
No related tags found
No related merge requests found
......@@ -35,34 +35,38 @@ define('xwiki-realtime-typingTests', function() {
};
}
function testInput(rootElement, textNode, offset, callback) {
let i = 0,
j = offset,
textInput = " The quick red fox jumps over the lazy brown dog.",
interval;
const cancel = function() {
interval.cancel();
};
function testInput(getSelection, callback) {
let i = 0, textInput = " The quick red fox jumps over the lazy brown dog.";
interval = setRandomizedInterval(function() {
callback();
let interval = setRandomizedInterval(() => {
const selection = getSelection();
if (selection?.rangeCount) {
const range = selection.getRangeAt(0);
if (range.collapsed && range.startContainer.nodeType === Node.TEXT_NODE) {
// "Type" the next character from the input string.
const textNode = range.startContainer;
textNode.insertData(range.startOffset, textInput.charAt(i));
if (textNode?.isConnected && textNode?.nodeType === Node.TEXT_NODE) {
// "Type" the next character from the text input.
textNode.insertData(Math.min(j, textNode.length), textInput.charAt(i));
} else {
// Continue typing in a new text node.
textNode = document.createTextNode('');
rootElement.appendChild(textNode);
j = -1;
}
// Type again the text input when we finish it.
i = (i + 1) % textInput.length;
// Type again the text input when we finish it.
i = (i + 1) % textInput.length;
j++;
// Update the caret position.
range.setStart(textNode, range.startOffset + 1);
range.collapse();
selection.removeAllRanges();
selection.addRange(range);
// Notify about the change.
callback();
}
}
}, 200, 50);
return {cancel};
return {
cancel: () => {
interval.cancel();
}
};
}
return {
......
......@@ -596,16 +596,10 @@ define('xwiki-realtime-wysiwyg', [
}
_easyTest() {
let container, offset;
const selection = this._editor.getSelection();
const range = selection?.rangeCount && selection.getRangeAt(0);
if (range) {
container = range.startContainer;
offset = range.startOffset;
}
const test = TypingTest.testInput(this._editor.getContentWrapper(), container, offset, this._onLocal.bind(this));
this._onLocal();
return test;
return TypingTest.testInput(
() => this._editor.getContentWrapper()?.ownerDocument.defaultView.getSelection(),
this._onLocal.bind(this)
);
}
_getXPath(element) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment