-
Notifications
You must be signed in to change notification settings - Fork 14
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
FEATURE: Add Ctrl+k keyboard shortcut handler #67
Open
gradinarufelix
wants to merge
3
commits into
sitegeist:main
Choose a base branch
from
gradinarufelix:feature-keyboard-shortcut
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@sitegeist/archaeopteryx-shortcut-handler", | ||
"license": "MIT", | ||
"version": "0.0.0", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
"build": "rm -rf lib && tsc -p tsconfig.build.json", | ||
"watch": "tsc -w -p tsconfig.build.json" | ||
}, | ||
"dependencies": { | ||
"@neos-project/neos-ui-extensibility": "^7.0.2", | ||
"@sitegeist/archaeopteryx-neos-bridge": "^0.0.0", | ||
"@sitegeist/archaeopteryx-core": "^0.0.0", | ||
"react": "^17.0.2" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.2.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
declare module '@neos-project/neos-ui-redux-store'; | ||
declare class NeosEditor { | ||
keystrokes: EditingKeystrokeHandler; | ||
execute(command: string, ...args: any[]): void; | ||
neos: { | ||
editorOptions: { | ||
linking?: { | ||
anchor?: boolean | ||
title?: boolean | ||
relNofollow?: boolean | ||
targetBlank?: boolean | ||
startingPoint?: string | ||
'Sitegeist.Archaeopteryx'?: { | ||
linkTypes?: { | ||
[key: string]: object | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
editing: { | ||
view: { | ||
focus(): void; | ||
} | ||
} | ||
} | ||
declare class EditingKeystrokeHandler { | ||
set(keystroke: string | Array<string | number>, callback: EditingKeystrokeCallback, options: object = {}): void | ||
} | ||
declare function EditingKeystrokeCallback(_: any, cancel: () => void): void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import {INeosContextProperties} from "@sitegeist/archaeopteryx-neos-bridge"; | ||
import {IEditor} from "@sitegeist/archaeopteryx-core"; | ||
import {SynchronousRegistry} from "@neos-project/neos-ui-extensibility"; | ||
import {ILinkOptions} from "@sitegeist/archaeopteryx-core/lib/domain"; | ||
|
||
export const executeCommand = (editor: NeosEditor, command: string, argument: any, reFocusEditor = true) => { | ||
editor.execute(command, argument); | ||
if (reFocusEditor) { | ||
editor.editing.view.focus(); | ||
} | ||
}; | ||
export function registerShortcutHandler ( | ||
neosContextProperties: INeosContextProperties, | ||
editor: IEditor | ||
): void { | ||
const {globalRegistry, store} = neosContextProperties; | ||
const ckeditor5Registry = globalRegistry.get('ckEditor5'); | ||
if (!ckeditor5Registry) { | ||
console.warn('[Sitegeist.Archaeopteryx]: Could not find ckeditor5 registry.'); | ||
console.warn('[Sitegeist.Archaeopteryx]: Skipping registration of keyboard shortcut...'); | ||
return; | ||
} | ||
|
||
const ckeditor5Configuration = ckeditor5Registry.get<SynchronousRegistry<any>>('config') | ||
if (!ckeditor5Registry) { | ||
console.warn('[Sitegeist.Archaeopteryx]: Could not find ckeditor5 config registry.'); | ||
console.warn('[Sitegeist.Archaeopteryx]: Skipping registration of keyboard shortcut...'); | ||
return; | ||
} | ||
|
||
ckeditor5Configuration.set('keystrokes', (ckEditorConfiguration: { plugins: ((ckEditorInstance: NeosEditor) => void)[]; }) => { | ||
ckEditorConfiguration.plugins.push((ckEditorInstance) => { | ||
ckEditorInstance.keystrokes.set('Ctrl+k', async (_: any, cancel: () => void) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it easily possible to make the shortcut configurable in a configuration file? |
||
// Cancel keystroke event | ||
cancel(); | ||
|
||
const inlineEditorOptions = ckEditorInstance.neos.editorOptions; | ||
const editorOptions = { | ||
...inlineEditorOptions?.linking?.['Sitegeist.Archaeopteryx'], | ||
linkTypes: { | ||
...inlineEditorOptions?.linking?.['Sitegeist.Archaeopteryx']?.linkTypes | ||
} | ||
}; | ||
|
||
if (inlineEditorOptions?.linking?.startingPoint) { | ||
editorOptions.linkTypes['Sitegeist.Archaeopteryx:Node'] = { | ||
...editorOptions.linkTypes['Sitegeist.Archaeopteryx:Node'], | ||
startingPoint: | ||
(editorOptions.linkTypes['Sitegeist.Archaeopteryx:Node'] as any).startingPoint | ||
?? inlineEditorOptions.linking.startingPoint | ||
}; | ||
} | ||
|
||
const formattingUnderCursor = store.getState()?.ui?.contentCanvas?.formattingUnderCursor | ||
const link = (() => { | ||
if (formattingUnderCursor?.link) { | ||
const [href, anchor] = formattingUnderCursor.link.split('#'); | ||
return { | ||
href, | ||
options: { | ||
anchor, | ||
title: formattingUnderCursor.linkTitle, | ||
targetBlank: formattingUnderCursor.linkTargetBlank, | ||
relNofollow: formattingUnderCursor.linkRelNofollow | ||
} | ||
}; | ||
} | ||
|
||
return null; | ||
})(); | ||
const enabledLinkOptions = (() => { | ||
const enabledLinkOptions: (keyof ILinkOptions)[] = []; | ||
|
||
if (ckEditorInstance?.neos?.editorOptions?.linking?.anchor) { | ||
enabledLinkOptions.push('anchor'); | ||
} | ||
|
||
if (ckEditorInstance?.neos?.editorOptions?.linking?.title) { | ||
enabledLinkOptions.push('title'); | ||
} | ||
|
||
if (ckEditorInstance?.neos?.editorOptions?.linking?.relNofollow) { | ||
enabledLinkOptions.push('relNofollow'); | ||
} | ||
|
||
if (ckEditorInstance?.neos?.editorOptions?.linking?.targetBlank) { | ||
enabledLinkOptions.push('targetBlank'); | ||
} | ||
|
||
return enabledLinkOptions; | ||
})(); | ||
|
||
const result = await editor.tx.editLink(link, enabledLinkOptions, editorOptions); | ||
|
||
if (result.change) { | ||
if (result.value === null) { | ||
executeCommand(ckEditorInstance, 'linkTitle', false, false); | ||
executeCommand(ckEditorInstance, 'linkRelNofollow', false, false); | ||
executeCommand(ckEditorInstance, 'linkTargetBlank', false, false); | ||
executeCommand(ckEditorInstance, 'unlink', undefined, true); | ||
} else { | ||
executeCommand(ckEditorInstance, 'linkTitle', result.value.options?.title || false, false); | ||
executeCommand(ckEditorInstance, 'linkTargetBlank', result.value.options?.targetBlank ?? false, false); | ||
executeCommand(ckEditorInstance, 'linkRelNofollow', result.value.options?.relNofollow ?? false, false); | ||
|
||
if (result.value.options?.anchor) { | ||
executeCommand(ckEditorInstance, 'link', `${result.value.href}#${result.value.options?.anchor}`, true); | ||
} else { | ||
executeCommand(ckEditorInstance, 'link', result.value.href, true); | ||
} | ||
} | ||
} else { | ||
executeCommand(ckEditorInstance, 'undo', undefined, true); | ||
executeCommand(ckEditorInstance, 'redo', undefined, true); | ||
} | ||
}) | ||
}); | ||
return ckEditorConfiguration; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "./lib", | ||
"module": "CommonJS" | ||
}, | ||
"include": ["./src/**/*.*"] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should be
ckeditor5Configuration
, or not?