-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Add Keyboard Shortcut for the Color Picker #3867 #4028
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
base: develop-codemirror-v6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -258,6 +258,39 @@ function getFileEmmetConfig(fileName) { | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function getColorPickerAtSelection(view) { | ||||||||||
| const { head } = view.state.selection.main; | ||||||||||
| const { node } = view.domAtPos(head); | ||||||||||
|
|
||||||||||
| const startEl = | ||||||||||
| node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement; | ||||||||||
|
|
||||||||||
| const lineEl = startEl?.closest('.cm-line'); | ||||||||||
|
|
||||||||||
| return ( | ||||||||||
| lineEl?.querySelector('input[type="color"]:not(:disabled)') || | ||||||||||
| view.contentDOM.querySelector('input[type="color"]:not(:disabled)') | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function openColorPickerWithKeyboard(view) { | ||||||||||
| const picker = getColorPickerAtSelection(view); | ||||||||||
|
|
||||||||||
| if (!picker || picker.disabled) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| picker.focus(); | ||||||||||
|
|
||||||||||
| if (typeof picker.showPicker === 'function') { | ||||||||||
| picker.showPicker(); | ||||||||||
| } else { | ||||||||||
| picker.click(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Extra custom keymaps. | ||||||||||
| // TODO: We need to add sublime mappings + other missing extra mappings here. | ||||||||||
| const extraKeymaps = [{ key: 'Tab', run: insertTab, shift: indentLess }]; | ||||||||||
|
|
@@ -291,13 +324,26 @@ export function createNewFileState(filename, document, settings) { | |||||||||
|
|
||||||||||
| // Depending on the file mode, we have a different tidier function. | ||||||||||
| const mode = getFileMode(filename); | ||||||||||
| extraKeymaps.push({ | ||||||||||
| const fileExtraKeymaps = [...extraKeymaps]; | ||||||||||
|
|
||||||||||
| fileExtraKeymaps.push({ | ||||||||||
| key: 'Mod-k', | ||||||||||
| run: (view) => { | ||||||||||
| if (mode !== 'css') { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return openColorPickerWithKeyboard(view); | ||||||||||
|
||||||||||
| return openColorPickerWithKeyboard(view); | |
| // In CSS mode, always consume Mod-k after attempting to open the color picker | |
| openColorPickerWithKeyboard(view); | |
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getColorPickerAtSelectionalready filters out disabled inputs via:not(:disabled), butopenColorPickerWithKeyboardcheckspicker.disabledagain. Consider removing one of these checks (either allow disabled in the selector and keep the runtime check, or keep the selector and drop the redundantpicker.disabledcheck) to avoid duplication.