Skip to content

Commit

Permalink
feat(CodeBlock): unification of code block creation logic (#384)
Browse files Browse the repository at this point in the history
Unification between toolbar action and keymap handler
  • Loading branch information
d3m1d0v authored Sep 26, 2024
1 parent eef229a commit d139318
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
26 changes: 24 additions & 2 deletions src/extensions/markdown/CodeBlock/commands.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import {newlineInCode, setBlockType} from 'prosemirror-commands';
import type {Command} from 'prosemirror-state';

import type {ExtensionDeps} from '../../../core';
import {toParagraph} from '../../base/BaseSchema';

import {cbType} from './const';
import {codeBlockType} from './const';

export {newlineInCode};

export const resetCodeblock: Command = (state, dispatch, view) => {
const {selection} = state;
if (
selection.empty &&
selection.$from.parent.type === cbType(state.schema) &&
selection.$from.parent.type === codeBlockType(state.schema) &&
view?.endOfTextblock('backward', state)
) {
return toParagraph(state, dispatch, view);
}
return false;
};

export const setCodeBlockType =
({serializer}: ExtensionDeps): Command =>
(state, dispatch) => {
const nodeType = codeBlockType(state.schema);

if (!setBlockType(nodeType)(state)) return false;

if (dispatch) {
const markup = serializer.serialize(state.selection.content().content);
dispatch(
state.tr.replaceSelectionWith(
nodeType.createAndFill({}, markup ? state.schema.text(markup) : null)!,
),
);
}
return true;
};
1 change: 1 addition & 0 deletions src/extensions/markdown/CodeBlock/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
export const cbAction = 'toCodeBlock';
/** @deprecated Use `codeBlockType` instead */
export const cbType = codeBlockType;
export {codeBlockType};
35 changes: 9 additions & 26 deletions src/extensions/markdown/CodeBlock/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {newlineInCode, setBlockType} from 'prosemirror-commands';
import {Fragment, NodeType} from 'prosemirror-model';
import {Command} from 'prosemirror-state';
import type {NodeType} from 'prosemirror-model';
import {hasParentNodeOfType} from 'prosemirror-utils';

import type {Action, ExtensionAuto, Keymap} from '../../../core';
Expand All @@ -10,8 +8,8 @@ import {withLogAction} from '../../../utils/keymap';

import {CodeBlockHighlight, HighlightLangMap} from './CodeBlockHighlight/CodeBlockHighlight';
import {CodeBlockSpecs, CodeBlockSpecsOptions} from './CodeBlockSpecs';
import {resetCodeblock} from './commands';
import {cbAction, cbType} from './const';
import {newlineInCode, resetCodeblock, setCodeBlockType} from './commands';
import {cbAction, codeBlockType} from './const';
import {codeBlockPastePlugin} from './plugins/codeBlockPastePlugin';

export {resetCodeblock} from './commands';
Expand All @@ -34,33 +32,18 @@ export const CodeBlock: ExtensionAuto<CodeBlockOptions> = (builder, opts) => {
const {codeBlockKey} = opts;
const bindings: Keymap = {Enter: newlineInCode, Backspace: resetCodeblock};
if (codeBlockKey) {
bindings[codeBlockKey] = withLogAction('code_block', setBlockType(cbType(deps.schema)));
bindings[codeBlockKey] = withLogAction('code_block', setCodeBlockType(deps));
}
return bindings;
});

builder.addInputRules(({schema}) => ({rules: [codeBlockRule(cbType(schema))]}));
builder.addAction(cbAction, ({schema, serializer}) => {
const cb = cbType(schema);
const cmd: Command = (state, dispatch) => {
if (!setBlockType(cb)(state)) return false;
if (dispatch) {
const markup = serializer.serialize(
Fragment.from(state.selection.content().content),
);
dispatch(
state.tr.replaceSelectionWith(
cb.createAndFill({}, markup ? state.schema.text(markup) : null)!,
),
);
}
return true;
};

builder.addInputRules(({schema}) => ({rules: [codeBlockRule(codeBlockType(schema))]}));
builder.addAction(cbAction, (deps) => {
const cb = codeBlockType(deps.schema);
return {
isActive: (state) => hasParentNodeOfType(cb)(state.selection),
isEnable: cmd,
run: cmd,
isEnable: setCodeBlockType(deps),
run: setCodeBlockType(deps),
};
});

Expand Down

0 comments on commit d139318

Please sign in to comment.