Skip to content
Merged
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
1 change: 1 addition & 0 deletions demo/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const enum PreviewType {

logger.setLogger({
metrics: console.info,
action: console.info,
...console,
});

Expand Down
5 changes: 4 additions & 1 deletion src/extensions/base/BaseSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {Command} from 'prosemirror-state';
import {setBlockType} from 'prosemirror-commands';
import {hasParentNodeOfType} from 'prosemirror-utils';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {BaseSchemaSpecs, BaseSchemaSpecsOptions, pType} from './BaseSchemaSpecs';

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

const {paragraphKey} = opts;
if (paragraphKey) {
builder.addKeymap(({schema}) => ({[paragraphKey]: setBlockType(pType(schema))}));
builder.addKeymap(({schema}) => ({
[paragraphKey]: withLogAction('paragraph', setBlockType(pType(schema))),
}));
}

builder.addAction(pAction, ({schema}) => {
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/behavior/History/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {Command} from 'prosemirror-state';
import {history, redo, undo} from 'prosemirror-history';
import type {Action, ActionSpec, ExtensionAuto, Keymap} from '../../../core';
import {withLogAction} from '../../../utils/keymap';

enum HistoryAction {
Undo = 'undo',
Expand All @@ -18,8 +19,8 @@ export const History: ExtensionAuto<HistoryOptions> = (builder, opts) => {
builder.addKeymap(() => {
const {undoKey, redoKey} = opts ?? {};
const bindings: Keymap = {};
if (undoKey) bindings[undoKey] = undo;
if (redoKey) bindings[redoKey] = redo;
if (undoKey) bindings[undoKey] = withLogAction('undo', undo);
if (redoKey) bindings[redoKey] = withLogAction('redo', redo);
return bindings;
});
builder
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Blockquote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {NodeType} from 'prosemirror-model';
import {wrappingInputRule} from 'prosemirror-inputrules';
import {hasParentNodeOfType} from 'prosemirror-utils';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {liftFromQuote, toggleQuote, joinPrevQuote} from './commands';
import {BlockquoteSpecs, blockquoteType} from './BlockquoteSpecs';

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

if (opts?.qouteKey) {
const {qouteKey} = opts;
builder.addKeymap(({schema}) => ({[qouteKey]: wrapIn(blockquoteType(schema))}));
builder.addKeymap(({schema}) => ({
[qouteKey]: withLogAction('blockquote', wrapIn(blockquoteType(schema))),
}));
}

builder.addKeymap(() => ({
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Bold/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {toggleMark} from 'prosemirror-commands';
import {withLogAction} from '../../../utils/keymap';
import {createToggleMarkAction} from '../../../utils/actions';
import type {Action, ExtensionAuto} from '../../../core';
import {markInputRule} from '../../../utils/inputrules';
Expand All @@ -19,7 +20,9 @@ export const Bold: ExtensionAuto<BoldOptions> = (builder, opts) => {

if (opts?.boldKey) {
const {boldKey} = opts;
builder.addKeymap(({schema}) => ({[boldKey]: toggleMark(boldType(schema))}));
builder.addKeymap(({schema}) => ({
[boldKey]: withLogAction('bold', toggleMark(boldType(schema))),
}));
}

builder.addInputRules(({schema}) => ({
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Code/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {toggleMark} from 'prosemirror-commands';
import codemark from 'prosemirror-codemark';
import {Plugin} from 'prosemirror-state';
import {withLogAction} from '../../../utils/keymap';
import {createToggleMarkAction} from '../../../utils/actions';
import type {Action, ExtensionAuto} from '../../../core';
import {codeMarkName, CodeSpecs, codeType} from './CodeSpecs';
Expand All @@ -22,7 +23,9 @@ export const Code: ExtensionAuto<CodeOptions> = (builder, opts) => {

if (opts?.codeKey) {
const {codeKey} = opts;
builder.addKeymap(({schema}) => ({[codeKey]: toggleMark(codeType(schema))}));
builder.addKeymap(({schema}) => ({
[codeKey]: withLogAction('code_inline', toggleMark(codeType(schema))),
}));
}

builder
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/CodeBlock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {textblockTypeInputRule} from 'prosemirror-inputrules';
import {Fragment, NodeType, Slice} from 'prosemirror-model';
import {Command, Plugin} from 'prosemirror-state';
import {hasParentNodeOfType} from 'prosemirror-utils';
import {withLogAction} from '../../../utils/keymap';
import type {Action, ExtensionAuto, Keymap} from '../../../core';
import {CodeBlockSpecs, CodeBlockSpecsOptions} from './CodeBlockSpecs';
import {resetCodeblock} from './commands';
Expand All @@ -22,7 +23,9 @@ export const CodeBlock: ExtensionAuto<CodeBlockOptions> = (builder, opts) => {
builder.addKeymap((deps) => {
const {codeBlockKey} = opts;
const bindings: Keymap = {Backspace: resetCodeblock};
if (codeBlockKey) bindings[codeBlockKey] = setBlockType(cbType(deps.schema));
if (codeBlockKey) {
bindings[codeBlockKey] = withLogAction('code_block', setBlockType(cbType(deps.schema)));
}
return bindings;
});

Expand Down
13 changes: 7 additions & 6 deletions src/extensions/markdown/Heading/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {setBlockType} from 'prosemirror-commands';
import type {Action, ExtensionAuto, Keymap} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {headingAction} from './actions';
import {HeadingAction, HeadingLevel, headingLevelAttr} from './const';
import {headingRule, hType} from './utils';
Expand Down Expand Up @@ -29,12 +30,12 @@ export const Heading: ExtensionAuto<HeadingOptions> = (builder, opts) => {
setBlockType(hType(schema), {[headingLevelAttr]: level});

const bindings: Keymap = {Backspace: resetHeading};
if (h1Key) bindings[h1Key] = cmd4lvl(1);
if (h2Key) bindings[h2Key] = cmd4lvl(2);
if (h3Key) bindings[h3Key] = cmd4lvl(3);
if (h4Key) bindings[h4Key] = cmd4lvl(4);
if (h5Key) bindings[h5Key] = cmd4lvl(5);
if (h6Key) bindings[h6Key] = cmd4lvl(6);
if (h1Key) bindings[h1Key] = withLogAction('heading1', cmd4lvl(1));
if (h2Key) bindings[h2Key] = withLogAction('heading2', cmd4lvl(2));
if (h3Key) bindings[h3Key] = withLogAction('heading3', cmd4lvl(3));
if (h4Key) bindings[h4Key] = withLogAction('heading4', cmd4lvl(4));
if (h5Key) bindings[h5Key] = withLogAction('heading5', cmd4lvl(5));
if (h6Key) bindings[h6Key] = withLogAction('heading6', cmd4lvl(6));
return bindings;
})
.addInputRules(({schema}) => ({rules: [headingRule(hType(schema), 6)]}));
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Italic/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {toggleMark} from 'prosemirror-commands';
import {withLogAction} from '../../../utils/keymap';
import {createToggleMarkAction} from '../../../utils/actions';
import type {Action, ExtensionAuto} from '../../../core';
import {markInputRule} from '../../../utils/inputrules';
Expand Down Expand Up @@ -28,7 +29,9 @@ export const Italic: ExtensionAuto<ItalicOptions> = (builder, opts) => {

if (opts?.italicKey) {
const {italicKey} = opts;
builder.addKeymap(({schema}) => ({[italicKey]: toggleMark(italicType(schema))}));
builder.addKeymap(({schema}) => ({
[italicKey]: withLogAction('italic', toggleMark(italicType(schema))),
}));
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/extensions/markdown/Lists/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {chainCommands} from 'prosemirror-commands';
import {liftListItem, sinkListItem, splitListItem} from 'prosemirror-schema-list';
import type {Action, ExtensionAuto, Keymap} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {actions} from './actions';
import {ListAction} from './const';
import {ListsInputRulesExtension, ListsInputRulesOptions} from './inputrules';
Expand All @@ -21,8 +22,8 @@ export const Lists: ExtensionAuto<ListsOptions> = (builder, opts) => {
builder.addKeymap(({schema}) => {
const {ulKey, olKey} = opts ?? {};
const bindings: Keymap = {};
if (ulKey) bindings[ulKey] = toList(blType(schema));
if (olKey) bindings[olKey] = toList(olType(schema));
if (ulKey) bindings[ulKey] = withLogAction('bulletList', toList(blType(schema)));
if (olKey) bindings[olKey] = withLogAction('orderedList', toList(olType(schema)));

return {
Enter: splitListItem(liType(schema)),
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Strike/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {toggleMark} from 'prosemirror-commands';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {createToggleMarkAction} from '../../../utils/actions';
import {markInputRule} from '../../../utils/inputrules';
import {strikeMarkName, StrikeSpecs, strikeType} from './StrikeSpecs';
Expand All @@ -26,7 +27,9 @@ export const Strike: ExtensionAuto<StrikeOptions> = (builder, opts) => {

if (opts?.strikeKey) {
const {strikeKey} = opts;
builder.addKeymap(({schema}) => ({[strikeKey]: toggleMark(strikeType(schema))}));
builder.addKeymap(({schema}) => ({
[strikeKey]: withLogAction('strike', toggleMark(strikeType(schema))),
}));
}
};

Expand Down
5 changes: 4 additions & 1 deletion src/extensions/markdown/Underline/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {toggleMark} from 'prosemirror-commands';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {createToggleMarkAction} from '../../../utils/actions';
import {markInputRule} from '../../../utils/inputrules';
import {underlineMarkName, UnderlineSpecs, underlineType} from './UnderlineSpecs';
Expand All @@ -26,7 +27,9 @@ export const Underline: ExtensionAuto<UnderlineOptions> = (builder, opts) => {

if (opts?.underlineKey) {
const {underlineKey} = opts;
builder.addKeymap(({schema}) => ({[underlineKey]: toggleMark(underlineType(schema))}));
builder.addKeymap(({schema}) => ({
[underlineKey]: withLogAction('underline', toggleMark(underlineType(schema))),
}));
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/extensions/yfm/YfmCut/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {chainCommands} from 'prosemirror-commands';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {nodeInputRule} from '../../../utils/inputrules';
import {cutType} from './const';
import {createYfmCut, toYfmCut} from './actions/toYfmCut';
Expand Down Expand Up @@ -48,7 +49,7 @@ export const YfmCut: ExtensionAuto<YfmCutOptions> = (builder, opts) => {

if (opts?.yfmCutKey) {
const {yfmCutKey} = opts;
builder.addKeymap(() => ({[yfmCutKey]: createYfmCut}));
builder.addKeymap(() => ({[yfmCutKey]: withLogAction('yfm_cut', createYfmCut)}));
}
};

Expand Down
13 changes: 7 additions & 6 deletions src/extensions/yfm/YfmHeading/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Action, ExtensionAuto, Keymap} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {HeadingAction} from './const';
import {headingRule, hType} from './YfmHeadingSpecs/utils';
import {headingAction} from './actions';
Expand All @@ -24,12 +25,12 @@ export const YfmHeading: ExtensionAuto<YfmHeadingOptions> = (builder, opts) => {
.addKeymap(() => {
const {h1Key, h2Key, h3Key, h4Key, h5Key, h6Key} = opts ?? {};
const bindings: Keymap = {Backspace: resetHeading};
if (h1Key) bindings[h1Key] = toHeading(1);
if (h2Key) bindings[h2Key] = toHeading(2);
if (h3Key) bindings[h3Key] = toHeading(3);
if (h4Key) bindings[h4Key] = toHeading(4);
if (h5Key) bindings[h5Key] = toHeading(5);
if (h6Key) bindings[h6Key] = toHeading(6);
if (h1Key) bindings[h1Key] = withLogAction('heading1', toHeading(1));
if (h2Key) bindings[h2Key] = withLogAction('heading2', toHeading(2));
if (h3Key) bindings[h3Key] = withLogAction('heading3', toHeading(3));
if (h4Key) bindings[h4Key] = withLogAction('heading4', toHeading(4));
if (h5Key) bindings[h5Key] = withLogAction('heading5', toHeading(5));
if (h6Key) bindings[h6Key] = withLogAction('heading6', toHeading(6));
return bindings;
})
.addInputRules(({schema}) => ({rules: [headingRule(hType(schema), 6)]}));
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/yfm/YfmNote/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {chainCommands} from 'prosemirror-commands';
import type {Action, ExtensionAuto} from '../../../core';
import {withLogAction} from '../../../utils/keymap';
import {createYfmNote, toYfmNote} from './actions/toYfmNote';
import {nodeInputRule} from '../../../utils/inputrules';
import {backToNoteTitle, exitFromNoteTitle, removeNote} from './commands';
Expand Down Expand Up @@ -31,7 +32,7 @@ export const YfmNote: ExtensionAuto<YfmNoteOptions> = (builder, opts) => {

if (opts?.yfmNoteKey) {
const {yfmNoteKey} = opts;
builder.addKeymap(() => ({[yfmNoteKey]: createYfmNote}));
builder.addKeymap(() => ({[yfmNoteKey]: withLogAction('yfm_note', createYfmNote)}));
}
};

Expand Down
12 changes: 12 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ declare global {
meta?: Record<string, any>;
};

interface ActionData {
action: string;
source: string;
[key: string]: any;
}

interface Logger {
log(...data: any[]): void;
info(...data: any[]): void;
error(...data: any[]): void;
warn(...data: any[]): void;
metrics(data: MetricsData): void;
action(data: ActionData): void;
}

interface Settings extends Partial<Logger> {}
Expand Down Expand Up @@ -44,6 +51,10 @@ class Logger implements YfmEditorLogger.Logger {
return this.#logger.metrics;
}

get action() {
return this.#logger.action;
}

setLogger(settings: YfmEditorLogger.Settings = {}) {
this.#logger = this.createLogger(settings);
}
Expand All @@ -60,6 +71,7 @@ class Logger implements YfmEditorLogger.Logger {
warn: settings.warn ?? noop,
error: settings.error ?? noop,
metrics: settings.metrics ?? noop,
action: settings.action ?? noop,
};
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/keymap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type {Command} from 'prosemirror-state';
import {logger} from '../logger';

export function withLogAction(action: string, command: Command): Command {
return (...args) => {
const res = command(...args);
if (res) logger.action({action, source: 'keymap'});
return res;
};
}