Skip to content

Commit

Permalink
e
Browse files Browse the repository at this point in the history
  • Loading branch information
winetree94 committed Dec 21, 2023
1 parent a0d8ad8 commit 5f3c4e8
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 75 deletions.
3 changes: 3 additions & 0 deletions packages/docs/docs/packages/heading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const schema = new Schema({
...edimParagraphNodes(),
...edimHeadingNodes({
nodeName: 'heading',
allowAlign: true,
levels: [2, 4, 6],
}),
},
});
Expand Down Expand Up @@ -84,6 +86,7 @@ const plugins: Plugin[] = [
}),
...edimCorePlugins(),
];

const view = new EditorView(document.querySelector('#editor'), {
state: EditorState.create({
schema: schema,
Expand Down
2 changes: 2 additions & 0 deletions packages/docs/src/components/examples/packages/heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const schema = new Schema({
...edimParagraphNodes(),
...edimHeadingNodes({
nodeName: 'heading',
allowAlign: true,
levels: [2, 4, 6],
}),
},
});
Expand Down
2 changes: 0 additions & 2 deletions packages/font-family/src/schemas/font-family.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const edimFontFamilyMarks = (
acc[font.fontFamily] = font;
return acc;
}, {});
console.log(fontByNames);

const markSpec: EdimFontFamilyMarkSpec = {
fonts: mergedConfigs.fonts,
Expand All @@ -82,7 +81,6 @@ export const edimFontFamilyMarks = (
}

if (mergedConfigs.fonts && !fontByNames[fontFamily]) {
console.log('false');
return false;
}
return {
Expand Down
1 change: 0 additions & 1 deletion packages/heading/src/plugins/heading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { NodeType } from 'prosemirror-model';

export interface EdimHeadingPluginConfigs {
nodeType: NodeType;
level?: number;
}

export const edimHeadingPlugins = (configs: EdimHeadingPluginConfigs) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/heading/src/plugins/input-rules.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Plugin as PMPlugin } from 'prosemirror-state';
import { NodeType } from 'prosemirror-model';
import { inputRules, textblockTypeInputRule } from 'prosemirror-inputrules';
import { EDIM_DEFAULT_HEADING_LEVEL } from '../schemas';
import { EdimHeadingNodeSpec } from '../schemas';

export interface EdimHeadingInputRulePluginConfigs {
nodeType: NodeType;
level?: number;
}

export const edimHeadingInputRulePlugins = (
configs: EdimHeadingInputRulePluginConfigs,
): PMPlugin[] => {
const level = configs?.level || EDIM_DEFAULT_HEADING_LEVEL;
const levels = (configs.nodeType.spec as EdimHeadingNodeSpec).meta.levels;
const levelRegex = levels.map((level) => `#{${level}}`).join('|');
return [
inputRules({
rules: [
textblockTypeInputRule(
new RegExp('^(#{1,' + level + '})\\s$'),
new RegExp(`^(${levelRegex})\\s`),
configs.nodeType,
(match) => ({ level: match[1].length }),
),
Expand Down
13 changes: 6 additions & 7 deletions packages/heading/src/plugins/keymaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import { setBlockType } from 'prosemirror-commands';
import { keymap } from 'prosemirror-keymap';
import { NodeType } from 'prosemirror-model';
import { Plugin as PMPlugin, Command } from 'prosemirror-state';
import { EDIM_DEFAULT_HEADING_LEVEL } from '../schemas';
import { EdimHeadingNodeSpec } from '../schemas';

export interface EdimHeadingKeymapPluginConfigs {
nodeType: NodeType;
level?: number;
}

export const edimHeadingKeymapPlugins = (
configs: EdimHeadingKeymapPluginConfigs,
): PMPlugin[] => {
const level = configs?.level || EDIM_DEFAULT_HEADING_LEVEL;
const levels = (configs.nodeType.spec as EdimHeadingNodeSpec).meta.levels;
const headingKeymaps: Record<string, Command> = {};

for (let i = 1; i <= level; i++) {
headingKeymaps['Alt-Mod-' + i] = setBlockType(configs.nodeType, {
level: i,
levels.forEach((level) => {
headingKeymaps['Alt-Mod-' + level] = setBlockType(configs.nodeType, {
level: level,
});
}
});

return [keymap(headingKeymaps)];
};
33 changes: 19 additions & 14 deletions packages/heading/src/schemas/heading.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { parseQuillTextAlign } from '@edim-editor/core';
import { NodeSpec } from 'prosemirror-model';

export const EDIM_HEADING_DEFAULT_NODE_NAME = 'heading';
export const EDIM_DEFAULT_HEADING_LEVEL = 6;

export type EdimHeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
export type EdimHeadingAlign = 'left' | 'right' | 'center' | null;

export const EDIM_HEADING_DEFAULT_NODE_NAME = 'heading';
export const EDIM_HEADING_ALLOWED_LEVELS: EdimHeadingLevel[] = [
1, 2, 3, 4, 5, 6,
];

export interface EdimHeadingAttrs {
level: EdimHeadingLevel;
align: EdimHeadingAlign;
Expand All @@ -23,21 +25,18 @@ export interface EdimHeadingNodeSpec extends NodeSpec {
};
meta: {
levels: EdimHeadingLevel[];
maxIndent: number;
};
}

export interface EdimHeadingNodeConfigs {
allowIndent?: boolean;
allowAlign?: boolean;
level?: EdimHeadingLevel[];
levels?: EdimHeadingLevel[];
nodeName?: string;
}

const EDIM_DEFAULT_HEADING_NODE_CONFIGS: Required<EdimHeadingNodeConfigs> = {
allowIndent: true,
allowAlign: true,
level: [1, 2, 3, 4, 5, 6],
levels: EDIM_HEADING_ALLOWED_LEVELS.slice(),
nodeName: EDIM_HEADING_DEFAULT_NODE_NAME,
};

Expand All @@ -49,33 +48,39 @@ export const edimHeadingNodes = (
...configs,
};

if (
!mergedConfigs.levels.every((level) =>
EDIM_HEADING_ALLOWED_LEVELS.includes(level),
)
) {
throw new Error('Invalid heading levels');
}

const nodeSpec: EdimHeadingNodeSpec = {
attrs: {
level: {
default: 1,
default: mergedConfigs.levels[0],
},
align: {
default: 'left',
},
},
meta: {
levels: mergedConfigs.level,
maxIndent: 6,
levels: mergedConfigs.levels,
},
content: 'inline*',
group: 'block',
defining: true,
parseDOM: [1, 2, 3, 4, 5, 6].map((level) => ({
parseDOM: mergedConfigs.levels.map((level) => ({
tag: `h${level}`,
getAttrs: (node) => {
const dom = node as HTMLElement;
const align = dom.getAttribute('data-text-align');
const quillAlign = parseQuillTextAlign(dom);
const indent = Number(dom.getAttribute('data-indent'));

return {
level,
align: align || quillAlign || null,
indent: indent || 0,
};
},
})),
Expand Down
6 changes: 4 additions & 2 deletions packages/menubar/src/components/text-type/text-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
classes,
html,
} from '@edim-editor/ui';
import { EdimHeadingLevel } from '@edim-editor/heading';
import { EdimHeadingNodeSpec } from '@edim-editor/heading';
import { mac, transformRangeToBlock } from '@edim-editor/core';
import { getTextType } from '../../utils';

Expand All @@ -21,10 +21,12 @@ export const EdimMenubarTextTypeSelect = () => {

const paragraphNodeType = context.options.textType.paragraphNodeType;
const headingNodeType = context.options.textType.headingNodeType;
const headingNodeSpec = headingNodeType.spec as EdimHeadingNodeSpec;

const textType = getTextType(context.editorView.state);

const textTypeOptions = [
...([1, 2, 3, 4, 5, 6] as EdimHeadingLevel[]).map((level) => ({
...headingNodeSpec.meta.levels.map((level) => ({
value: `h${level}`,
label: `Heading ${level}`,
Element: EdimHeadingByNumber[level],
Expand Down
44 changes: 0 additions & 44 deletions packages/menubar/src/utils/current-font.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/menubar/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './current-text-type';
export * from './current-align-type';
export * from './current-font';

0 comments on commit 5f3c4e8

Please sign in to comment.