-
Notifications
You must be signed in to change notification settings - Fork 4
feat(core): extract ShortcutsPlugin into @editorjs/shortcuts-plugin #180
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
Open
ilyamore88
wants to merge
1
commit into
main
Choose a base branch
from
worktree-shortcuts-plugin-extraction
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
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 hidden or 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: Shortcuts plugin check | ||
| on: | ||
| pull_request: | ||
| merge_group: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| issues: write | ||
| actions: read | ||
|
|
||
| jobs: | ||
| package-check: | ||
| uses: ./.github/workflows/package-check.yml | ||
| with: | ||
| package-name: '@editorjs/shortcuts-plugin' | ||
| working-directory: './packages/plugins/shortcuts-plugin' | ||
| include-mutations: true | ||
| secrets: | ||
| stryker_dashboard_api_key: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ jest-report.json | |
| **/.env | ||
| /--help/ | ||
| .claude/settings.local.json | ||
| .claude/worktrees/ | ||
This file contains hidden or 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 hidden or 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,49 @@ | ||
| # Shortcuts Plugin | ||
|
|
||
| ## Purpose | ||
|
|
||
| `@editorjs/shortcuts-plugin` is a built-in `EditorjsPlugin` that maps keyboard shortcuts declared in a tool's `options.shortcut` to inline-tool application through the `EditorAPI`. It subscribes to tool-loaded events to collect shortcuts and to delegated keydown events to dispatch them. | ||
|
|
||
| ## Requirements | ||
|
|
||
| ### Requirement: Keyboard shortcuts plugin | ||
| The system SHALL provide a `ShortcutsPlugin` (an `EditorjsPlugin`) that, on construction, subscribes to the `core:tool:loaded` and `ui:key-down` events, registers each loaded tool's string `options.shortcut`, and applies the matching inline tool to the current selection via the `EditorAPI` when its shortcut is pressed. | ||
|
|
||
| #### Scenario: Registering a shortcut from a loaded tool | ||
| - **GIVEN** a tool is loaded whose merged `options.shortcut` is a string (e.g. `CMD+B`) | ||
| - **WHEN** the `core:tool:loaded` event fires | ||
| - **THEN** the plugin registers a mapping from that shortcut string to the tool's name | ||
|
|
||
| #### Scenario: Ignoring tools without a string shortcut | ||
| - **GIVEN** a tool is loaded whose `options.shortcut` is absent or is not a string | ||
| - **WHEN** the `core:tool:loaded` event fires | ||
| - **THEN** the plugin registers no shortcut for that tool | ||
|
|
||
| #### Scenario: Triggering an inline tool via shortcut | ||
| - **GIVEN** an inline tool is registered with `options.shortcut` set to a key combination (e.g. `CMD+B`) | ||
| - **WHEN** that key combination is pressed while the editor has focus | ||
| - **THEN** the plugin prevents the native event's default action and applies the corresponding inline tool to the current selection via `api.selection.applyInlineTool` | ||
|
|
||
| #### Scenario: Matching only the first registered shortcut | ||
| - **GIVEN** more than one registered shortcut would match the keydown | ||
| - **WHEN** the `ui:key-down` event fires | ||
| - **THEN** the plugin applies only the first matching tool and stops | ||
|
|
||
| #### Scenario: Ignoring keydown during IME composition | ||
| - **GIVEN** the native keydown event has `isComposing === true` | ||
| - **WHEN** the `ui:key-down` event fires | ||
| - **THEN** the plugin performs no matching and leaves the native event untouched | ||
|
|
||
| #### Scenario: Tolerating a missing caret when applying a tool | ||
| - **GIVEN** applying the inline tool throws an `IndexError` (e.g. no caret in a text input) | ||
| - **WHEN** a matching shortcut is dispatched | ||
| - **THEN** the plugin swallows the error and leaves the editor unchanged, while any other error propagates | ||
|
|
||
| #### Scenario: Releasing shortcuts on destroy | ||
| - **GIVEN** a `ShortcutsPlugin` instance has registered shortcuts | ||
| - **WHEN** `destroy()` is called | ||
| - **THEN** it clears the registered shortcuts so subsequent keydowns dispatch nothing | ||
|
|
||
| Shortcuts for block tools and block tunes (a `shortcuts` map in tool `options`) are reserved for future work and not yet implemented. | ||
|
|
||
| Implemented in `src/index.ts`, validated by its co-located `.spec.ts`. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| .yarn/* | ||
| !.yarn/patches | ||
| !.yarn/plugins | ||
| !.yarn/releases | ||
| !.yarn/sdks | ||
| !.yarn/versions | ||
|
|
||
| # Swap the comments on the following lines if you don't wish to use zero-installs | ||
| # Documentation here: https://yarnpkg.com/features/zero-installs | ||
| #!.yarn/cache | ||
| #.pnp.* | ||
|
|
||
| # IDE | ||
| .idea/* | ||
|
|
||
| node_modules/* | ||
| dist/* | ||
|
|
||
| # tests | ||
| coverage/ | ||
| reports/ | ||
|
|
||
| # stryker temp files | ||
| .stryker-tmp |
This file contains hidden or 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,3 @@ | ||
| # Shortcuts Plugin | ||
|
|
||
| Built-in Editor.js Plugin that maps keyboard shortcuts declared in a tool's `options.shortcut` to inline-tool application. |
This file contains hidden or 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,39 @@ | ||
| import CodeX from 'eslint-config-codex'; | ||
|
|
||
| export default [ | ||
| ...CodeX, | ||
| { | ||
| languageOptions: { | ||
| parserOptions: { | ||
| project: './tsconfig.eslint.json', | ||
| tsconfigRootDir: import.meta.dirname, | ||
| sourceType: 'module', | ||
| }, | ||
| }, | ||
| rules: { | ||
| 'n/no-unpublished-import': ['error', { | ||
| allowModules: [ | ||
| 'eslint-config-codex', | ||
| ], | ||
| ignoreTypeImport: true, | ||
| }], | ||
| 'n/no-missing-import': 'off', | ||
| '@typescript-eslint/unbound-method': 'off', | ||
| 'n/no-unsupported-features/node-builtins': ['error', { | ||
| version: '>=24.0.0', | ||
| ignores: [], | ||
| }], | ||
| }, | ||
| }, | ||
| { | ||
| files: ['**/*.spec.ts'], | ||
| rules: { | ||
| /** | ||
| * For test files allow dev dependencies imports | ||
| */ | ||
| 'n/no-unpublished-import': ['error', { | ||
| allowModules: ['@jest/globals'], | ||
| }], | ||
| }, | ||
| }, | ||
| ]; |
This file contains hidden or 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,31 @@ | ||
| import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
|
||
| export default { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| testMatch: [ '<rootDir>/src/**/*.spec.ts' ], | ||
| extensionsToTreatAsEsm: [ '.ts' ], | ||
| moduleNameMapper: { | ||
| '^(\\.{1,2}/.*)\\.js$': '$1', | ||
| }, | ||
| coverageReporters: ['lcov', 'json-summary', 'text-summary'], | ||
| transform: { | ||
| '^.+\\.tsx?$': [ | ||
| 'ts-jest', | ||
| { | ||
| useESM: true, | ||
| }, | ||
| ], | ||
| '^.+\\.jsx?$': [ | ||
| 'babel-jest', | ||
| { | ||
| presets: [ | ||
| ['@babel/preset-env', { targets: { node: 'current' } }], | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| transformIgnorePatterns: [ | ||
| 'node_modules/(?!@editorjs)', | ||
| ], | ||
| } as JestConfigWithTsJest; |
This file contains hidden or 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,42 @@ | ||
| { | ||
| "name": "@editorjs/shortcuts-plugin", | ||
| "version": "0.0.0", | ||
| "packageManager": "yarn@4.0.1", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "yarn clear && tsc --build tsconfig.build.json", | ||
| "build:declaration": "yarn build --emitDeclarationOnly", | ||
| "lint": "eslint ./src", | ||
| "lint:ci": "yarn lint --max-warnings 0", | ||
| "lint:fix": "yarn lint --fix", | ||
| "test": "node --experimental-vm-modules $(yarn bin jest)", | ||
| "test:coverage": "yarn test --coverage=true", | ||
| "test:mutations": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" stryker run", | ||
| "clear": "rm -rf dist && rm -f tsconfig.build.tsbuildinfo && rm -f tsconfig.tsbuildinfo" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/core": "^7.29.0", | ||
| "@babel/preset-env": "^7.29.2", | ||
| "@jest/globals": "^29.7.0", | ||
| "@stryker-mutator/core": "^7.0.2", | ||
| "@stryker-mutator/jest-runner": "^7.0.2", | ||
| "@stryker-mutator/typescript-checker": "^7.0.2", | ||
| "@types/eslint": "^9.6.1", | ||
| "@types/jest": "^29.5.12", | ||
| "@types/node": "^22.10.2", | ||
| "babel-jest": "^29.7.0", | ||
| "eslint": "^9.24.0", | ||
| "eslint-config-codex": "^2.0.3", | ||
| "eslint-plugin-import": "^2.31.0", | ||
| "jest": "^29.7.0", | ||
| "stryker-cli": "^1.0.2", | ||
| "ts-jest": "^29.2.5", | ||
| "ts-node": "^10.9.2", | ||
| "typescript": "^5.5.4" | ||
| }, | ||
| "dependencies": { | ||
| "@editorjs/sdk": "workspace:^" | ||
| } | ||
| } |
Oops, something went wrong.
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.
Looks unrelated. I mean there is bunch of claude things to be git-ignored (e.g.
.claude/sessions,.claude/scheduled_tasks.lock, etc).Worth investigate it and split those changes into separate PR?