Skip to content

Commit 20a2657

Browse files
authored
feat: add logging keymap actions (#106)
1 parent db35e5b commit 20a2657

File tree

17 files changed

+79
-26
lines changed

17 files changed

+79
-26
lines changed

demo/Playground.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const enum PreviewType {
4242

4343
logger.setLogger({
4444
metrics: console.info,
45+
action: console.info,
4546
...console,
4647
});
4748

src/extensions/base/BaseSchema/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {Command} from 'prosemirror-state';
22
import {setBlockType} from 'prosemirror-commands';
33
import {hasParentNodeOfType} from 'prosemirror-utils';
44
import type {Action, ExtensionAuto} from '../../../core';
5+
import {withLogAction} from '../../../utils/keymap';
56
import {BaseSchemaSpecs, BaseSchemaSpecsOptions, pType} from './BaseSchemaSpecs';
67

78
export {BaseNode, pType} from './BaseSchemaSpecs';
@@ -20,7 +21,9 @@ export const BaseSchema: ExtensionAuto<BaseSchemaOptions> = (builder, opts) => {
2021

2122
const {paragraphKey} = opts;
2223
if (paragraphKey) {
23-
builder.addKeymap(({schema}) => ({[paragraphKey]: setBlockType(pType(schema))}));
24+
builder.addKeymap(({schema}) => ({
25+
[paragraphKey]: withLogAction('paragraph', setBlockType(pType(schema))),
26+
}));
2427
}
2528

2629
builder.addAction(pAction, ({schema}) => {

src/extensions/behavior/History/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {Command} from 'prosemirror-state';
22
import {history, redo, undo} from 'prosemirror-history';
33
import type {Action, ActionSpec, ExtensionAuto, Keymap} from '../../../core';
4+
import {withLogAction} from '../../../utils/keymap';
45

56
enum HistoryAction {
67
Undo = 'undo',
@@ -18,8 +19,8 @@ export const History: ExtensionAuto<HistoryOptions> = (builder, opts) => {
1819
builder.addKeymap(() => {
1920
const {undoKey, redoKey} = opts ?? {};
2021
const bindings: Keymap = {};
21-
if (undoKey) bindings[undoKey] = undo;
22-
if (redoKey) bindings[redoKey] = redo;
22+
if (undoKey) bindings[undoKey] = withLogAction('undo', undo);
23+
if (redoKey) bindings[redoKey] = withLogAction('redo', redo);
2324
return bindings;
2425
});
2526
builder

src/extensions/markdown/Blockquote/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {NodeType} from 'prosemirror-model';
33
import {wrappingInputRule} from 'prosemirror-inputrules';
44
import {hasParentNodeOfType} from 'prosemirror-utils';
55
import type {Action, ExtensionAuto} from '../../../core';
6+
import {withLogAction} from '../../../utils/keymap';
67
import {liftFromQuote, toggleQuote, joinPrevQuote} from './commands';
78
import {BlockquoteSpecs, blockquoteType} from './BlockquoteSpecs';
89

@@ -18,7 +19,9 @@ export const Blockquote: ExtensionAuto<BlockquoteOptions> = (builder, opts) => {
1819

1920
if (opts?.qouteKey) {
2021
const {qouteKey} = opts;
21-
builder.addKeymap(({schema}) => ({[qouteKey]: wrapIn(blockquoteType(schema))}));
22+
builder.addKeymap(({schema}) => ({
23+
[qouteKey]: withLogAction('blockquote', wrapIn(blockquoteType(schema))),
24+
}));
2225
}
2326

2427
builder.addKeymap(() => ({

src/extensions/markdown/Bold/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {toggleMark} from 'prosemirror-commands';
2+
import {withLogAction} from '../../../utils/keymap';
23
import {createToggleMarkAction} from '../../../utils/actions';
34
import type {Action, ExtensionAuto} from '../../../core';
45
import {markInputRule} from '../../../utils/inputrules';
@@ -19,7 +20,9 @@ export const Bold: ExtensionAuto<BoldOptions> = (builder, opts) => {
1920

2021
if (opts?.boldKey) {
2122
const {boldKey} = opts;
22-
builder.addKeymap(({schema}) => ({[boldKey]: toggleMark(boldType(schema))}));
23+
builder.addKeymap(({schema}) => ({
24+
[boldKey]: withLogAction('bold', toggleMark(boldType(schema))),
25+
}));
2326
}
2427

2528
builder.addInputRules(({schema}) => ({

src/extensions/markdown/Code/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {toggleMark} from 'prosemirror-commands';
22
import codemark from 'prosemirror-codemark';
33
import {Plugin} from 'prosemirror-state';
4+
import {withLogAction} from '../../../utils/keymap';
45
import {createToggleMarkAction} from '../../../utils/actions';
56
import type {Action, ExtensionAuto} from '../../../core';
67
import {codeMarkName, CodeSpecs, codeType} from './CodeSpecs';
@@ -22,7 +23,9 @@ export const Code: ExtensionAuto<CodeOptions> = (builder, opts) => {
2223

2324
if (opts?.codeKey) {
2425
const {codeKey} = opts;
25-
builder.addKeymap(({schema}) => ({[codeKey]: toggleMark(codeType(schema))}));
26+
builder.addKeymap(({schema}) => ({
27+
[codeKey]: withLogAction('code_inline', toggleMark(codeType(schema))),
28+
}));
2629
}
2730

2831
builder

src/extensions/markdown/CodeBlock/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {textblockTypeInputRule} from 'prosemirror-inputrules';
33
import {Fragment, NodeType, Slice} from 'prosemirror-model';
44
import {Command, Plugin} from 'prosemirror-state';
55
import {hasParentNodeOfType} from 'prosemirror-utils';
6+
import {withLogAction} from '../../../utils/keymap';
67
import type {Action, ExtensionAuto, Keymap} from '../../../core';
78
import {CodeBlockSpecs, CodeBlockSpecsOptions} from './CodeBlockSpecs';
89
import {resetCodeblock} from './commands';
@@ -22,7 +23,9 @@ export const CodeBlock: ExtensionAuto<CodeBlockOptions> = (builder, opts) => {
2223
builder.addKeymap((deps) => {
2324
const {codeBlockKey} = opts;
2425
const bindings: Keymap = {Backspace: resetCodeblock};
25-
if (codeBlockKey) bindings[codeBlockKey] = setBlockType(cbType(deps.schema));
26+
if (codeBlockKey) {
27+
bindings[codeBlockKey] = withLogAction('code_block', setBlockType(cbType(deps.schema)));
28+
}
2629
return bindings;
2730
});
2831

src/extensions/markdown/Heading/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {setBlockType} from 'prosemirror-commands';
22
import type {Action, ExtensionAuto, Keymap} from '../../../core';
3+
import {withLogAction} from '../../../utils/keymap';
34
import {headingAction} from './actions';
45
import {HeadingAction, HeadingLevel, headingLevelAttr} from './const';
56
import {headingRule, hType} from './utils';
@@ -29,12 +30,12 @@ export const Heading: ExtensionAuto<HeadingOptions> = (builder, opts) => {
2930
setBlockType(hType(schema), {[headingLevelAttr]: level});
3031

3132
const bindings: Keymap = {Backspace: resetHeading};
32-
if (h1Key) bindings[h1Key] = cmd4lvl(1);
33-
if (h2Key) bindings[h2Key] = cmd4lvl(2);
34-
if (h3Key) bindings[h3Key] = cmd4lvl(3);
35-
if (h4Key) bindings[h4Key] = cmd4lvl(4);
36-
if (h5Key) bindings[h5Key] = cmd4lvl(5);
37-
if (h6Key) bindings[h6Key] = cmd4lvl(6);
33+
if (h1Key) bindings[h1Key] = withLogAction('heading1', cmd4lvl(1));
34+
if (h2Key) bindings[h2Key] = withLogAction('heading2', cmd4lvl(2));
35+
if (h3Key) bindings[h3Key] = withLogAction('heading3', cmd4lvl(3));
36+
if (h4Key) bindings[h4Key] = withLogAction('heading4', cmd4lvl(4));
37+
if (h5Key) bindings[h5Key] = withLogAction('heading5', cmd4lvl(5));
38+
if (h6Key) bindings[h6Key] = withLogAction('heading6', cmd4lvl(6));
3839
return bindings;
3940
})
4041
.addInputRules(({schema}) => ({rules: [headingRule(hType(schema), 6)]}));

src/extensions/markdown/Italic/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {toggleMark} from 'prosemirror-commands';
2+
import {withLogAction} from '../../../utils/keymap';
23
import {createToggleMarkAction} from '../../../utils/actions';
34
import type {Action, ExtensionAuto} from '../../../core';
45
import {markInputRule} from '../../../utils/inputrules';
@@ -28,7 +29,9 @@ export const Italic: ExtensionAuto<ItalicOptions> = (builder, opts) => {
2829

2930
if (opts?.italicKey) {
3031
const {italicKey} = opts;
31-
builder.addKeymap(({schema}) => ({[italicKey]: toggleMark(italicType(schema))}));
32+
builder.addKeymap(({schema}) => ({
33+
[italicKey]: withLogAction('italic', toggleMark(italicType(schema))),
34+
}));
3235
}
3336
};
3437

src/extensions/markdown/Lists/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {chainCommands} from 'prosemirror-commands';
22
import {liftListItem, sinkListItem, splitListItem} from 'prosemirror-schema-list';
33
import type {Action, ExtensionAuto, Keymap} from '../../../core';
4+
import {withLogAction} from '../../../utils/keymap';
45
import {actions} from './actions';
56
import {ListAction} from './const';
67
import {ListsInputRulesExtension, ListsInputRulesOptions} from './inputrules';
@@ -21,8 +22,8 @@ export const Lists: ExtensionAuto<ListsOptions> = (builder, opts) => {
2122
builder.addKeymap(({schema}) => {
2223
const {ulKey, olKey} = opts ?? {};
2324
const bindings: Keymap = {};
24-
if (ulKey) bindings[ulKey] = toList(blType(schema));
25-
if (olKey) bindings[olKey] = toList(olType(schema));
25+
if (ulKey) bindings[ulKey] = withLogAction('bulletList', toList(blType(schema)));
26+
if (olKey) bindings[olKey] = withLogAction('orderedList', toList(olType(schema)));
2627

2728
return {
2829
Enter: splitListItem(liType(schema)),

0 commit comments

Comments
 (0)