Skip to content

Commit 488cf6e

Browse files
committed
Finish deciding, testing and documenting Ctrl/Cmd difference (Fixes #200)
1 parent b822318 commit 488cf6e

File tree

6 files changed

+51
-90
lines changed

6 files changed

+51
-90
lines changed

code-input.d.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ export namespace plugins {
129129

130130
// ESM-SUPPORT-START-PLUGIN-find-and-replace Do not (re)move this - it's needed for ESM generation
131131
/**
132-
* Add Find-and-Replace (Ctrl+F for find, Ctrl+H for replace by default) functionality to the code editor.
132+
* Add Find-and-Replace (Ctrl/Cmd+F for find, Ctrl+H for replace by default) functionality to the code editor.
133133
* Files: find-and-replace.js / find-and-replace.css
134134
*/
135135
class FindAndReplace extends Plugin {
136136
/**
137-
* Create a find-and-replace command plugin to pass into a template
138-
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
137+
* Create a find-and-replace command plugin to pass into a template. To ensure keyboard shortcuts remain intuitive, set the alwaysCtrl parameter to false.
138+
* @param {boolean} useCtrlF Should Ctrl/Cmd+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
139139
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
140140
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the find-and-replace.js source code for the English text.
141-
* @param {boolean} alwaysCtrl: if true always use Ctrl+F/H as the keyboard shortcut; if false use Cmd+F/H on Apple devices and Ctrl+F/H elsewhere. False highly recommended; defaults to true for backwards compatiblity.
141+
* @param {boolean} alwaysCtrl Setting this to false makes the keyboard shortcuts follow the operating system while avoiding clashes (right now: Cmd+F/Ctrl+H on Apple, Ctrl+F/Ctrl+H otherwise.) and is recommended; true forces Ctrl+F/Ctrl+H and is default for backwards compatibility.
142142
*/
143143
constructor(useCtrlF?: boolean, useCtrlH?: boolean,
144144
instructionTranslations?: {
@@ -174,13 +174,13 @@ export namespace plugins {
174174

175175
// ESM-SUPPORT-START-PLUGIN-go-to-line Do not (re)move this - it's needed for ESM generation
176176
/**
177-
* Add basic Go-To-Line (ctrl-G by default) functionality to the code editor.
177+
* Add Go-To-Line (Ctrl/Cmd+G by default) functionality to the code editor.
178178
* Files: go-to-line.js / go-to-line.css
179179
*/
180180
class GoToLine extends Plugin {
181181
/**
182-
* Create a go-to-line command plugin to pass into a template
183-
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
182+
* Create a go-to-line command plugin to pass into a template.
183+
* @param {boolean} useCtrlG Should Ctrl/Cmd+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
184184
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the English text.
185185
*/
186186
constructor(useCtrlG?: boolean,
@@ -193,7 +193,6 @@ export namespace plugins {
193193
guidanceValidLine?: (line: Number) => string;
194194
guidanceValidColumn?: (line: Number, column: Number) => string;
195195
},
196-
alwaysCtrl?: boolean
197196
);
198197
/**
199198
* Show a search-like dialog prompting line number.
@@ -217,7 +216,6 @@ export namespace plugins {
217216
* @param {Object} bracketPairs Opening brackets mapped to closing brackets, default and example {"(": ")", "[": "]", "{": "}"}. All brackets must only be one character, and this can be left as null to remove bracket-based indentation behaviour.
218217
* @param {boolean} escTabToChangeFocus Whether pressing the Escape key before (Shift+)Tab should make this keypress focus on a different element (Tab's default behaviour). You should always either enable this or use this plugin's disableTabIndentation and enableTabIndentation methods linked to other keyboard shortcuts, for accessibility.
219218
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the English text.
220-
* @param {boolean} alwaysCtrl: if true always use Ctrl+G as the keyboard shortcut; if false use Cmd+G on Apple devices and Ctrl+G elsewhere. False highly recommended; defaults to true for backwards compatiblity.
221219
*/
222220
constructor(defaultSpaces?: boolean, numSpaces?: Number, bracketPairs?: Object, escTabToChangeFocus?: boolean, instructionTranslations?: {
223221
tabForIndentation?: string;

docs/i18n/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ code-input:not(.code-input_registered)::after {
2020

2121
It is only present for debugging and explanatory purposes when highlighting cannot be seen, and should not contain important or specific information about the editor state; if you need such information, especially for screen reader users, add separate text to your application which disappears after registering the code-input without errors.
2222

23-
**Plugins** sometimes come with user interface features (e.g. the find-and-replace dialog) which contain text to be translated. The text is provided as an extra argument to the plugin constructor containing translated strings or functions to produce them for each translation key, with the keys and their English values found in either the `code-input.d.ts` or the plugin's source code file. Here's an example:
23+
**Plugins** sometimes come with user interface features (e.g. the find-and-replace dialog) which contain text to be translated. The text is provided as an argument to the plugin constructor containing translated strings or functions to produce them for each translation key, with the keys and their English values found in either the `code-input.d.ts` or the plugin's source code file. Here's an example:
2424
```javascript
2525
// CC-BY; Attribution: Translated by Oliver Geer with some help from English Wiktionary
2626
let findAndReplaceTranslations = {
@@ -45,7 +45,7 @@ let findAndReplaceTranslations = {
4545
};
4646
// ...
4747
// passed when the plugin is constructed:
48-
new codeInput.plugins.FindAndReplace(true, true, findAndReplaceTranslations),
48+
new codeInput.plugins.FindAndReplace(true, true, findAndReplaceTranslations, false),
4949
```
5050

5151
## Other

docs/plugins/_index.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ Right now, you can only add one plugin of each type (e.g. one SelectTokenCallbac
144144
145145
<script>
146146
let findAndReplacePlugin = new codeInput.plugins.FindAndReplace(
147-
true, // Should Ctrl+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
148-
true, // Should Ctrl+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
147+
true, // Should Ctrl/Cmd+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
148+
true, // Should Ctrl/Cmd+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
149149
{}, // Keep this as an empty object for the English UI, or add translations as in https://code-input-js.org/i18n/
150-
false // Should Ctrl key be forced for keyboard shortcuts; setting this to false makes the keyboard shortcut follow the operating system (i.e. Cmd on Apple).
150+
false // Setting this to false makes the keyboard shortcuts follow the operating system while avoiding clashes (right now: Cmd+F/Ctrl+H on Apple, Ctrl+F/Ctrl+H otherwise.) and is recommended; true forces Ctrl+F/Ctrl+H and is default for backwards compatibility.
151151
);
152152
// Programatically opening the dialogs, to integrate with your user interface
153153
function find() {
@@ -197,9 +197,8 @@ Hickory dickory dock.</code-input>
197197
198198
<script>
199199
let goToLinePlugin = new codeInput.plugins.GoToLine(
200-
true, // Should Ctrl+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
200+
true, // Should Ctrl/Cmd+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
201201
{}, // Keep this as an empty object for the English UI, or add translations as in https://code-input-js.org/i18n/
202-
false // Should Ctrl key be forced for keyboard shortcuts; setting this to false makes the keyboard shortcut follow the operating system (i.e. Cmd on Apple).
203202
);
204203
// Programatically opening the dialogs, to integrate with your user interface
205204
function goToLine() {

plugins/find-and-replace.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Add Find-and-Replace (Ctrl+F for find, Ctrl+H for replace by default) functionality to the code editor.
2+
* Add Find-and-Replace (Ctrl/Cmd+F for find, Ctrl+H for replace by default) functionality to the code editor.
33
* Files: find-and-replace.js / find-and-replace.css
44
*/
55
"use strict";
@@ -32,11 +32,11 @@ codeInput.plugins.FindAndReplace = class extends codeInput.Plugin {
3232
};
3333

3434
/**
35-
* Create a find-and-replace command plugin to pass into a template
36-
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
35+
* Create a find-and-replace command plugin to pass into a template. To ensure keyboard shortcuts remain intuitive, set the alwaysCtrl parameter to false.
36+
* @param {boolean} useCtrlF Should Ctrl/Cmd+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
3737
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
38-
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the find-and-replace.js source code for the English text and available keys.
39-
* @param {boolean} alwaysCtrl: if true always use Ctrl+F/H as the keyboard shortcut; if false use Cmd+F/H on Apple devices and Ctrl+F/H elsewhere. False highly recommended; defaults to true for backwards compatiblity.
38+
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the find-and-replace.js source code for the English text.
39+
* @param {boolean} alwaysCtrl Setting this to false makes the keyboard shortcuts follow the operating system while avoiding clashes (right now: Cmd+F/Ctrl+H on Apple, Ctrl+F/Ctrl+H otherwise.) and is recommended; true forces Ctrl+F/Ctrl+H and is default for backwards compatibility.
4040
*/
4141
constructor(useCtrlF = true, useCtrlH = true, instructionTranslations = {}, alwaysCtrl = true) {
4242
super([]); // No observed attributes
@@ -422,29 +422,24 @@ codeInput.plugins.FindAndReplace = class extends codeInput.Plugin {
422422
this.updateFindMatches(dialog);
423423
}
424424

425-
hasModifier(event) {
426-
if(this.alwaysCtrl) return event.ctrlKey;
427-
// Thanks to https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform
428-
if(navigator.platform.startsWith("Mac") || navigator.platform === "iPhone") {
429-
// Command
430-
return event.metaKey;
431-
} else {
432-
// Control
433-
return event.ctrlKey;
434-
}
435-
}
436-
437-
/* Event handler for keydown event that makes Ctrl+F open find dialog */
425+
/* Event handler for keydown event that makes Ctrl/Cmd+F open find dialog */
438426
checkCtrlF(codeInput, event) {
439-
if (this.hasModifier(event) && event.key == 'f') {
440-
event.preventDefault();
441-
this.showPrompt(codeInput, false);
427+
if(!this.alwaysCtrl && (navigator.platform.startsWith("Mac") || navigator.platform === "iPhone")) {
428+
if (event.metaKey && event.key == 'f') { // Cmd+F
429+
event.preventDefault();
430+
this.showPrompt(codeInput, false);
431+
}
432+
} else {
433+
if (event.ctrlKey && event.key == 'f') {
434+
event.preventDefault();
435+
this.showPrompt(codeInput, false);
436+
}
442437
}
443438
}
444439

445440
/* Event handler for keydown event that makes Ctrl+H open find+replace dialog */
446441
checkCtrlH(codeInput, event) {
447-
if (this.hasModifier(event) && event.key == 'h') {
442+
if (event.ctrlKey && event.key == 'h') {
448443
event.preventDefault();
449444
this.showPrompt(codeInput, true);
450445
}

plugins/go-to-line.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Add basic Go-To-Line (Ctrl+G by default) functionality to the code editor.
2+
* Add Go-To-Line (Ctrl/Cmd+G by default) functionality to the code editor.
33
* Files: go-to-line.js / go-to-line.css
44
*/
55
"use strict";
@@ -18,12 +18,11 @@ codeInput.plugins.GoToLine = class extends codeInput.Plugin {
1818
};
1919

2020
/**
21-
* Create a go-to-line command plugin to pass into a template
21+
* Create a go-to-line command plugin to pass into a template.
2222
* @param {boolean} useCtrlG Should Ctrl/Cmd+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
23-
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the available keys and English text.
24-
* @param {boolean} alwaysCtrl: if true always use Ctrl+G as the keyboard shortcut; if false use Cmd+G on Apple devices and Ctrl+G elsewhere. False highly recommended; defaults to true for backwards compatiblity.
23+
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the English text.
2524
*/
26-
constructor(useCtrlG = true, instructionTranslations = {}, alwaysCtrl = true) {
25+
constructor(useCtrlG = true, instructionTranslations = {}) {
2726
super([]); // No observed attributes
2827
this.useCtrlG = useCtrlG;
2928
this.addTranslations(this.instructions, instructionTranslations);
@@ -203,21 +202,9 @@ codeInput.plugins.GoToLine = class extends codeInput.Plugin {
203202
}
204203
}
205204

206-
hasModifier(event) {
207-
if(this.alwaysCtrl) return event.ctrlKey;
208-
// Thanks to https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform
209-
if(navigator.platform.startsWith("Mac") || navigator.platform === "iPhone") {
210-
// Command
211-
return event.metaKey;
212-
} else {
213-
// Control
214-
return event.ctrlKey;
215-
}
216-
}
217-
218205
/* Event handler for keydown event that makes Ctrl+G open go to line dialog */
219206
checkCtrlG(codeInput, event) {
220-
if (this.hasModifier(event) && event.key == 'g') {
207+
if (event.ctrlKey && event.key == 'g') {
221208
event.preventDefault();
222209
this.showPrompt(codeInput);
223210
}

0 commit comments

Comments
 (0)