Skip to content

Commit

Permalink
refactor: add presets like from markdown-it (#264)
Browse files Browse the repository at this point in the history
Added presets similar to presets in markdown-it  (zero, commonmark, default).

And two additional ones: yfm and full.
  • Loading branch information
d3m1d0v authored Jun 6, 2024
1 parent 2816c18 commit 47f6b79
Show file tree
Hide file tree
Showing 52 changed files with 438 additions and 367 deletions.
27 changes: 4 additions & 23 deletions src/bundle/wysiwyg-preset.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import {Node} from 'prosemirror-model';

import {ExtensionAuto} from '../core';
import {
BasePreset,
BasePresetOptions,
BehaviorPreset,
BehaviorPresetOptions,
MarkdownBlocksPreset,
MarkdownBlocksPresetOptions,
MarkdownMarksPreset,
MarkdownMarksPresetOptions,
YfmPreset,
YfmPresetOptions,
} from '../extensions';
import {BehaviorPreset, BehaviorPresetOptions} from '../extensions/behavior';
import {EditorModeKeymap, EditorModeKeymapOptions} from '../extensions/behavior/EditorModeKeymap';
import {BaseNode, YfmHeadingAttr, YfmNoteNode} from '../extensions/specs';
import {i18n as i18nPlaceholder} from '../i18n/placeholder';
import {FullPreset, FullPresetOptions} from '../presets/full';
import {Action as A, formatter as f} from '../shortcuts';
import type {FileUploadHandler} from '../utils/upload';

import {wCommandMenuConfig, wSelectionMenuConfig} from './config/wysiwyg';
import {emojiDefs} from './emoji';

export type ExtensionsOptions = BasePresetOptions &
MarkdownMarksPresetOptions &
Omit<MarkdownBlocksPresetOptions, 'image' | 'heading'> &
YfmPresetOptions &
BehaviorPresetOptions;
export type ExtensionsOptions = BehaviorPresetOptions & FullPresetOptions;

export type BundlePresetOptions = ExtensionsOptions &
EditorModeKeymapOptions & {
Expand Down Expand Up @@ -126,12 +112,7 @@ export const BundlePreset: ExtensionAuto<BundlePresetOptions> = (builder, opts)
},
};

builder
.use(BehaviorPreset, options)
.use(BasePreset, options)
.use(MarkdownMarksPreset, options)
.use(MarkdownBlocksPreset, {...options, image: false, heading: false})
.use(YfmPreset, options);
builder.use(BehaviorPreset, options).use(FullPreset, options);

const ignoreActions = [
A.Undo,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Command, EditorState, Selection, TextSelection, Transaction} from 'prose
import {builders} from 'prosemirror-test-builder';

import {ExtensionsManager} from '../core';
import {BaseNode, BaseSpecsPreset} from '../extensions/base/specs';
import {BaseNode, BaseSchemaSpecs} from '../extensions/base/specs';
import {
BlockquoteSpecs,
DeflistNode,
Expand All @@ -20,7 +20,7 @@ import {joinPreviousBlock} from './join';

const {schema} = new ExtensionsManager({
extensions: (builder) =>
builder.use(BaseSpecsPreset, {}).use(BlockquoteSpecs).use(Html).use(DeflistSpecs, {}),
builder.use(BaseSchemaSpecs, {}).use(BlockquoteSpecs).use(Html).use(DeflistSpecs, {}),
}).buildDeps();

const {doc, p, bq, htmlBlock, dList, dTerm, dDesc} = builders<
Expand Down
19 changes: 0 additions & 19 deletions src/extensions/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
import type {ExtensionAuto} from '../../core';

import {BaseInputRules} from './BaseInputRules';
import {BaseKeymap} from './BaseKeymap';
import {BaseSchema, BaseSchemaOptions} from './BaseSchema';
import {BaseStyles} from './BaseStyles';

export * from './BaseSchema';
export * from './BaseStyles';
export * from './BaseKeymap';
export * from './BaseInputRules';

export type BasePresetOptions = {
baseSchema?: BaseSchemaOptions;
};

export const BasePreset: ExtensionAuto<BasePresetOptions> = (builder, opts) => {
builder
.use(BaseSchema, opts.baseSchema ?? {})
.use(BaseKeymap)
.use(BaseInputRules)
.use(BaseStyles);
};
12 changes: 0 additions & 12 deletions src/extensions/base/specs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
import type {ExtensionAuto} from '../../core';

import {BaseSchemaSpecs, BaseSchemaSpecsOptions} from './BaseSchema/BaseSchemaSpecs';

export * from './BaseSchema/BaseSchemaSpecs';

export type BaseSpecPresetOptions = {
baseSchema?: BaseSchemaSpecsOptions;
};

export const BaseSpecsPreset: ExtensionAuto<BaseSpecPresetOptions> = (builder, opts) => {
builder.use(BaseSchemaSpecs, opts.baseSchema ?? {});
};
16 changes: 7 additions & 9 deletions src/extensions/behavior/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {ExtensionAuto} from '../../core';
import {PlaceholderOptions} from '../../utils/placeholder';
import {Emoji, EmojiOptions} from '../yfm/Emoji';

import {ClicksOnEdges} from './ClicksOnEdges';
import {Clipboard, ClipboardOptions} from './Clipboard';
Expand All @@ -14,12 +13,17 @@ import {Selection} from './Selection';
import {SelectionContext, SelectionContextOptions} from './SelectionContext';
import {WidgetDecoration} from './WidgetDecoration';

export * from './ClicksOnEdges';
export * from './Clipboard';
export * from './CommandMenu';
export * from './Cursor';
export * from './FilePaste';
export * from './History';
export * from './Clipboard';
export * from './Selection';
export * from './Placeholder';
export * from './ReactRenderer';
export * from './Selection';
export * from './SelectionContext';
export * from './WidgetDecoration';

export type BehaviorPresetOptions = {
cursor?: CursorOptions;
Expand All @@ -30,8 +34,6 @@ export type BehaviorPresetOptions = {
selectionContext?: SelectionContextOptions;

commandMenu?: CommandMenuOptions;

emoji?: EmojiOptions;
};

export const BehaviorPreset: ExtensionAuto<BehaviorPresetOptions> = (builder, opts) => {
Expand All @@ -45,10 +47,6 @@ export const BehaviorPreset: ExtensionAuto<BehaviorPresetOptions> = (builder, op
.use(WidgetDecoration)
.use(SelectionContext, opts.selectionContext ?? {});

if (opts.emoji) {
builder.use(Emoji, opts.emoji);
}

if (opts.commandMenu) builder.use(CommandMenu, opts.commandMenu);
builder.use(FilePaste);
builder.use(ClicksOnEdges);
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Blockquote/Blockquote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {BlockquoteSpecs, blockquoteNodeName} from './BlockquoteSpecs';

Expand All @@ -12,7 +12,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(BlockquoteSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(BlockquoteSpecs),
}).buildDeps();

const {doc, p, q} = builders<'doc' | 'p' | 'q'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Blockquote/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {builders} from 'prosemirror-test-builder';

import {ExtensionsManager} from '../../../core';
import {get$Cursor, isNodeSelection} from '../../../utils/selection';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';
import {DeflistNode, DeflistSpecs} from '../Deflist/DeflistSpecs';
import {Html, HtmlAttr, HtmlNode} from '../Html';

Expand All @@ -13,7 +13,7 @@ import {joinPrevQuote} from './commands';

const {schema} = new ExtensionsManager({
extensions: (builder) =>
builder.use(BaseSpecsPreset, {}).use(BlockquoteSpecs).use(Html).use(DeflistSpecs, {}),
builder.use(BaseSchemaSpecs, {}).use(BlockquoteSpecs).use(Html).use(DeflistSpecs, {}),
}).buildDeps();

const {doc, p, bq, htmlBlock, dList, dTerm, dDesc} = builders<
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Bold/Bold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {BoldSpecs, boldMarkName} from './BoldSpecs';

Expand All @@ -12,7 +12,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(BoldSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(BoldSpecs),
}).buildDeps();

const {doc, p, b} = builders<'doc' | 'p', 'b'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Breaks/Breaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';
import {BoldSpecs, boldMarkName} from '../../markdown/Bold/BoldSpecs';

import {BreakNodeName, BreaksSpecs} from './BreaksSpecs';
Expand All @@ -13,7 +13,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(BreaksSpecs, {}).use(BoldSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(BreaksSpecs, {}).use(BoldSpecs),
}).buildDeps();

const {doc, p, hb, sb, bold} = builders<'doc' | 'p' | 'hb' | 'sb', 'bold'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Code/Code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';
import {BoldSpecs, boldMarkName} from '../Bold/BoldSpecs';
import {ItalicSpecs, italicMarkName} from '../Italic/ItalicSpecs';

Expand All @@ -15,7 +15,7 @@ const {
serializer,
} = new ExtensionsManager({
extensions: (builder) =>
builder.use(BaseSpecsPreset, {}).use(BoldSpecs).use(CodeSpecs).use(ItalicSpecs),
builder.use(BaseSchemaSpecs, {}).use(BoldSpecs).use(CodeSpecs).use(ItalicSpecs),
}).buildDeps();

const {doc, p, b, i, c} = builders<'doc' | 'p', 'b' | 'i' | 'c'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/CodeBlock/CodeBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {CodeBlockNodeAttr, CodeBlockSpecs, codeBlockNodeName} from './CodeBlockSpecs';

Expand All @@ -12,7 +12,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(CodeBlockSpecs, {}),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(CodeBlockSpecs, {}),
}).buildDeps();

const {doc, p, cb} = builders<'doc' | 'p' | 'cb'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Deflist/Deflist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {DeflistNode, DeflistSpecs} from './DeflistSpecs';

Expand All @@ -12,7 +12,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(DeflistSpecs, {}),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(DeflistSpecs, {}),
}).buildDeps();

const {doc, p, dl, dt, dd} = builders<'doc' | 'p' | 'dl' | 'dt' | 'dd'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Heading/Heading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';
import {BoldSpecs, boldMarkName} from '../Bold/BoldSpecs';

import {HeadingSpecs} from './HeadingSpecs';
Expand All @@ -14,7 +14,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(HeadingSpecs, {}).use(BoldSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(HeadingSpecs, {}).use(BoldSpecs),
}).buildDeps();

const {doc, b, p, h, h1, h2, h3, h4, h5, h6} = builders<
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/HorizontalRule/HorizontalRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {
HorizontalRuleSpecs,
Expand All @@ -16,7 +16,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(HorizontalRuleSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(HorizontalRuleSpecs),
}).buildDeps();

const {doc, p, hr, hr2, hr3} = builders<'doc' | 'p' | 'hr' | 'hr2' | 'hr3'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Image/Image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {builders} from 'prosemirror-test-builder';

import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {ImageAttr, ImageSpecs, imageNodeName} from './ImageSpecs';

Expand All @@ -11,7 +11,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(ImageSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(ImageSpecs),
}).buildDeps();

const {doc, p, img, img2} = builders<'doc' | 'p' | 'img' | 'img2'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Italic/Italic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {builders} from 'prosemirror-test-builder';
import {parseDOM} from '../../../../tests/parse-dom';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {ItalicSpecs, italicMarkName} from './ItalicSpecs';

Expand All @@ -12,7 +12,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(ItalicSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(ItalicSpecs),
}).buildDeps();

const {doc, p, i} = builders<'doc' | 'p', 'i'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Link/Link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {builders} from 'prosemirror-test-builder';

import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {LinkAttr, LinkSpecs, linkMarkName} from './LinkSpecs';

Expand All @@ -11,7 +11,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(LinkSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(LinkSpecs),
}).buildDeps();

const {doc, p, a, lnk, lnk4} = builders<'doc' | 'p' | 'a' | 'lnk' | 'lnk4'>(schema, {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/markdown/Lists/Lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {builders} from 'prosemirror-test-builder';

import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {BaseNode, BaseSchemaSpecs} from '../../base/specs';

import {ListNode, ListsSpecs} from './ListsSpecs';

Expand All @@ -11,7 +11,7 @@ const {
markupParser: parser,
serializer,
} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(ListsSpecs),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(ListsSpecs),
}).buildDeps();

const {doc, p, li, ul, ol} = builders<'doc' | 'p' | 'li' | 'ul' | 'ol'>(schema, {
Expand Down
Loading

0 comments on commit 47f6b79

Please sign in to comment.