Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile selection change #5087

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/kitchen-sink/demo.js
Original file line number Diff line number Diff line change
@@ -376,6 +376,7 @@ optionsPanel.add({
var optionsPanelContainer = document.getElementById("optionsPanel");
optionsPanel.render();
optionsPanelContainer.insertBefore(optionsPanel.container, optionsPanelContainer.firstChild);
optionsPanel.container.style.width = "80%";
optionsPanel.on("setOption", function(e) {
util.saveOption(e.name, e.value);
});
@@ -451,7 +452,7 @@ env.editSnippets = function() {
};

optionsPanelContainer.insertBefore(
dom.buildDom(["div", {style: "text-align:right;margin-right: 60px"},
dom.buildDom(["div", {style: "text-align:right;width: 80%"},
["div", {},
["button", {onclick: env.editSnippets}, "Edit Snippets"]],
["div", {},
8 changes: 0 additions & 8 deletions kitchen-sink.html
Original file line number Diff line number Diff line change
@@ -6,16 +6,8 @@
<meta name="viewport" content="width=device-width,height=device-height" />
<title>Ace Kitchen Sink</title>
<meta name="author" content="Fabian Jakobs">
<!--
Ace
version %version%
commit %commit%
-->

<link rel="stylesheet" href="demo/kitchen-sink/styles.css" type="text/css" media="screen" charset="utf-8">
<!--PACKAGE
<script async="true" src="https://use.edgefonts.net/source-code-pro.js"></script>
PACKAGE-->

<link href="./doc/site/images/favicon.ico" rel="icon" type="image/x-icon">
</head>
24 changes: 24 additions & 0 deletions src/keyboard/textinput.js
Original file line number Diff line number Diff line change
@@ -69,6 +69,8 @@ var TextInput = function(parentNode, host) {
if (ignoreFocusEvents) return;
host.onBlur(e);
isFocused = false;
if (isMobile && !isIOS)
document.removeEventListener("selectionchange", detectSelectionChange);
}, host);
event.addListener(text, "focus", function(e) {
if (ignoreFocusEvents) return;
@@ -85,6 +87,8 @@ var TextInput = function(parentNode, host) {
setTimeout(resetSelection);
else
resetSelection();
if (isMobile && !isIOS)
document.addEventListener("selectionchange", detectSelectionChange);
}, host);
this.$focusScroll = false;
this.focus = function() {
@@ -270,6 +274,26 @@ var TextInput = function(parentNode, host) {
}
};

function detectSelectionChange(e) {
if (!text || !text.parentNode)
document.removeEventListener("selectionchange", detectSelectionChange);
if (inComposition) return;

if (text.selectionStart !== text.selectionEnd) return;
var startDiff = text.selectionStart - lastSelectionStart;
var oldLenght = lastSelectionEnd - lastSelectionStart;
if (startDiff > 0) {
startDiff = Math.max(startDiff - oldLenght, 1);
} else if (startDiff === 0 && oldLenght) {
startDiff = -1;
}
var repeat = Math.abs(startDiff);
var key = startDiff > 0 ? KEYS.right : KEYS.left;
for (var i = 0; i < repeat; i++) {
host.onCommandKey({}, 0, key);
}
}

var inputHandler = null;
this.setInputHandler = function(cb) {inputHandler = cb;};
this.getInputHandler = function() {return inputHandler;};
40 changes: 40 additions & 0 deletions src/keyboard/textinput_test.js
Original file line number Diff line number Diff line change
@@ -153,6 +153,46 @@ module.exports = {
assert.equal(editor.getValue(), "y");
},

"test: android spacebar moves cursor": function() {
setUserAgentForTests(true, false);
var value = "Juhu kinners!";
editor.setValue(value);
editor.blur();
editor.focus();
var lastCommand = "";
editor.commands.on("exec", function(e) {
lastCommand += e.command.name;
});

textarea.selectionStart = textarea.selectionEnd;
document.dispatchEvent(new CustomEvent("selectionchange"));
assert.equal(lastCommand, "gotoright");
lastCommand = "";

textarea.selectionStart =
textarea.selectionEnd = textarea.selectionStart - 1;
document.dispatchEvent(new CustomEvent("selectionchange"));
assert.equal(lastCommand, "gotoleft");
lastCommand = "";

assert.equal(editor.getSelectedText(), "");
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
textarea.dispatchEvent(new CustomEvent("select"));
assert.equal(editor.getSelectedText(), value);

textarea.selectionEnd = textarea.selectionStart;
document.dispatchEvent(new CustomEvent("selectionchange"));
assert.equal(lastCommand, "gotoleft");
lastCommand = "";

textarea.selectionStart =
textarea.selectionEnd = textarea.selectionEnd + 2;
document.dispatchEvent(new CustomEvent("selectionchange"));
assert.equal(lastCommand, "gotorightgotoright");
lastCommand = "";
},

"test: composition with visible textarea": function() {
var data = [
// select ll
2 changes: 1 addition & 1 deletion src/test/mockdom.js
Original file line number Diff line number Diff line change
@@ -443,7 +443,7 @@ function Node(name) {
if (!e.timeStamp) e.timeStamp = Date.now();
e.currentTarget = this;
var events = this._events && this._events[e.type];
events && events.forEach(function(listener) {
events && events.slice().forEach(function(listener) {
listener.call(this, e);
}, this);
if (this["on" + e.type])