Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/shortcuts-plugin.yml
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 }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jest-report.json
**/.env
/--help/
.claude/settings.local.json
.claude/worktrees/
10 changes: 0 additions & 10 deletions openspec/specs/core/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,3 @@ The system SHALL provide `UndoRedoManager`, which batches consecutive model even
- **THEN** the default undo/redo behavior is suppressed

Implemented in `src/components/UndoRedoManager.ts`, validated by its co-located `.spec.ts`.

### Requirement: Keyboard shortcuts plugin
The system SHALL provide a `ShortcutsPlugin` (an `EditorjsPlugin`) that maps keyboard shortcuts declared in a tool's `options.shortcut` to inline-tool application through the `EditorAPI`.

#### 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** `ShortcutsPlugin` applies the corresponding inline tool to the current selection via the `EditorAPI`

Implemented in `src/plugins/ShortcutsPlugin.ts`.
49 changes: 49 additions & 0 deletions openspec/specs/shortcuts-plugin/spec.md
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`.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@editorjs/model": "workspace:^",
"@editorjs/paragraph": "workspace:^",
"@editorjs/sdk": "workspace:^",
"@editorjs/shortcuts-plugin": "workspace:^",
"inversify": "^8.1.0",
"reflect-metadata": "^0.2.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Paragraph } from '@editorjs/paragraph';
import { BoldInlineTool } from '@editorjs/bold';
import { ItalicInlineTool } from '@editorjs/italic';
import { LinkInlineTool } from '@editorjs/inline-link';
import { ShortcutsPlugin } from './plugins/ShortcutsPlugin.js';
import { ShortcutsPlugin } from '@editorjs/shortcuts-plugin';
import { DOMAdapters } from '@editorjs/dom-adapters';
import { BlocksManager } from './components/BlockManager.js';
import { BlockRenderer } from './components/BlockRenderer.js';
Expand Down
3 changes: 3 additions & 0 deletions packages/core/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
},
{
"path": "../plugins/clipboard-plugin/tsconfig.build.json"
},
{
"path": "../plugins/shortcuts-plugin/tsconfig.build.json"
}
]
}
3 changes: 3 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
},
{
"path": "../plugins/clipboard-plugin/tsconfig.build.json"
},
{
"path": "../plugins/shortcuts-plugin/tsconfig.build.json"
}
]
}
24 changes: 24 additions & 0 deletions packages/plugins/shortcuts-plugin/.gitignore
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
3 changes: 3 additions & 0 deletions packages/plugins/shortcuts-plugin/README.md
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.
39 changes: 39 additions & 0 deletions packages/plugins/shortcuts-plugin/eslint.config.mjs
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'],
}],
},
},
];
31 changes: 31 additions & 0 deletions packages/plugins/shortcuts-plugin/jest.config.ts
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;
42 changes: 42 additions & 0 deletions packages/plugins/shortcuts-plugin/package.json
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:^"
}
}
Loading
Loading