Skip to content

Commit fd27d23

Browse files
authored
Merge pull request #4028 from AlexanderORuban/feature/color-picker-keybind
fix: Add Keyboard Shortcut for the Color Picker #3867
2 parents dafb44f + b1c13fc commit fd27d23

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

client/modules/IDE/components/Editor/stateUtils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,35 @@ function getFileEmmetConfig(fileName) {
236236
}
237237
}
238238

239+
function getColorPickerAtSelection(view) {
240+
const { head } = view.state.selection.main;
241+
const { node } = view.domAtPos(head);
242+
243+
const startEl =
244+
node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
245+
246+
const lineEl = startEl?.closest('.cm-line');
247+
248+
return (
249+
lineEl?.querySelector('input[type="color"]:not(:disabled)') ||
250+
view.contentDOM.querySelector('input[type="color"]:not(:disabled)')
251+
);
252+
}
253+
254+
function openColorPickerWithKeyboard(view) {
255+
const picker = getColorPickerAtSelection(view);
256+
257+
if (!picker || picker.disabled) {
258+
return false;
259+
}
260+
261+
picker.focus();
262+
263+
if (typeof picker.showPicker === 'function') {
264+
picker.showPicker();
265+
} else {
266+
picker.click();
267+
}
239268
function focusOnReferenceArrow(view) {
240269
if (completionStatus(view.state) !== 'active') return false;
241270

@@ -395,6 +424,13 @@ export function createNewFileState(filename, document, settings) {
395424
// Keep this binding local to each file state so modes don't accumulate
396425
// across files via a shared module-level array.
397426
const mode = getFileMode(filename);
427+
428+
let colorPickerKeymap = [];
429+
if (mode === 'css' || mode === 'javascript') {
430+
colorPickerKeymap.push({
431+
key: 'Mod-k',
432+
run: (view) => openColorPickerWithKeyboard(view)
433+
});
398434

399435
// Make a keymap for both uppercase and lowercase F, since
400436
// since browsers can differ in which one they send for the Shift-Mod-F shortcut.
@@ -417,6 +453,7 @@ export function createNewFileState(filename, document, settings) {
417453

418454
const keymaps = [
419455
extraKeymaps,
456+
colorPickerKeymap,
420457
fileTidyKeymap,
421458
closeBracketsKeymap,
422459
defaultKeymap,

0 commit comments

Comments
 (0)