Skip to content

Commit

Permalink
refactor: revamp type structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Jul 27, 2019
1 parent d06d2bb commit 876d7b6
Show file tree
Hide file tree
Showing 59 changed files with 1,047 additions and 1,415 deletions.
15 changes: 15 additions & 0 deletions @remirror/core-extensions/src/core-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ export const baseExtensions: PrioritizedExtension[] = [
{ extension: new DropCursorExtension(), priority: 10 },
{ extension: new BaseKeymapExtension(), priority: 10 },
];

/**
* The list of extensions provided by default in a remirror editor. This is useful for extending with your own types
* to provide automated typechecking.
*/
export type BaseExtensionList = [
DocExtension,
TextExtension,
ParagraphExtension,
CompositionExtension,
HistoryExtension,
GapCursorExtension,
DropCursorExtension,
BaseKeymapExtension,
];
29 changes: 23 additions & 6 deletions @remirror/core-extensions/src/extensions/history-extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
BaseExtensionOptions,
BooleanExtensionCheck,
CommandFunction,
environment,
Extension,
Expand All @@ -27,15 +26,13 @@ export interface HistoryExtensionOptions extends BaseExtensionOptions {
newGroupDelay?: number | null;
}

type HistoryExtensionCommands = 'undo' | 'redo';

/**
* This extension provides undo and redo commands and inserts a plugin which
* handles history related actions.
*
* @builtin
*/
export class HistoryExtension extends Extension<HistoryExtensionOptions, HistoryExtensionCommands, {}> {
export class HistoryExtension extends Extension<HistoryExtensionOptions> {
get name() {
return 'history' as const;
}
Expand Down Expand Up @@ -80,8 +77,8 @@ export class HistoryExtension extends Extension<HistoryExtensionOptions, History
* - Redo is not enabled when at the end of the history and there is nothing left to redo.
* - Undo is not enabled when at the beginning of the history and there is nothing left to undo.
*/
public isEnabled({ getState }: ExtensionManagerParams): BooleanExtensionCheck<HistoryExtensionCommands> {
return ({ command }) => {
public isEnabled({ getState }: ExtensionManagerParams) {
return ({ command }: { command?: string }) => {
switch (command) {
case 'undo':
return undoDepth(getState()) > 0;
Expand All @@ -105,7 +102,27 @@ export class HistoryExtension extends Extension<HistoryExtensionOptions, History
*/
public commands() {
return {
/**
* Undo the last action that occurred. This can be overriden by
* setting an `"addToHistory"` [metadata property](#state.Transaction.setMeta) of `false` on a transaction
* to prevent it from being rolled back by undo.
*
* ```ts
* actions.undo()
*
* // To prevent this use
* tr.setMeta(pluginKey, { addToHistory: false })
* ```
*/
undo: () => undo,

/**
* Redo an action that was in the undo stack.
*
* ```ts
* actions.redo()
* ```
*/
redo: () => redo,
};
}
Expand Down
8 changes: 4 additions & 4 deletions @remirror/core-extensions/src/marks/bold-extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
CommandMarkTypeParams,
ExtensionManagerMarkTypeParams,
isElementDOMNode,
isString,
MarkExtension,
MarkExtensionOptions,
MarkExtensionSpec,
MarkGroup,
markInputRule,
SchemaMarkTypeParams,
} from '@remirror/core';
import { toggleMark } from 'prosemirror-commands';

export class BoldExtension extends MarkExtension<MarkExtensionOptions, 'bold', {}> {
export class BoldExtension extends MarkExtension<MarkExtensionOptions> {
get name() {
return 'bold' as const;
}
Expand Down Expand Up @@ -39,7 +39,7 @@ export class BoldExtension extends MarkExtension<MarkExtensionOptions, 'bold', {
};
}

public keys({ type }: SchemaMarkTypeParams) {
public keys({ type }: ExtensionManagerMarkTypeParams) {
return {
'Mod-b': toggleMark(type),
};
Expand All @@ -53,7 +53,7 @@ export class BoldExtension extends MarkExtension<MarkExtensionOptions, 'bold', {
};
}

public inputRules({ type }: SchemaMarkTypeParams) {
public inputRules({ type }: ExtensionManagerMarkTypeParams) {
return [markInputRule({ regexp: /(?:\*\*|__)([^*_]+)(?:\*\*|__)$/, type })];
}
}
10 changes: 5 additions & 5 deletions @remirror/core-extensions/src/marks/code-extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
CommandMarkTypeParams,
ExtensionManagerMarkTypeParams,
MarkExtension,
MarkExtensionOptions,
MarkExtensionSpec,
MarkGroup,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
} from '@remirror/core';
import { toggleMark } from 'prosemirror-commands';

export class CodeExtension extends MarkExtension<MarkExtensionOptions, 'code', {}> {
export class CodeExtension extends MarkExtension<MarkExtensionOptions> {
get name() {
return 'code' as const;
}
Expand All @@ -23,7 +23,7 @@ export class CodeExtension extends MarkExtension<MarkExtensionOptions, 'code', {
};
}

public keys({ type }: SchemaMarkTypeParams) {
public keys({ type }: ExtensionManagerMarkTypeParams) {
return {
'Mod-`': toggleMark(type),
};
Expand All @@ -33,11 +33,11 @@ export class CodeExtension extends MarkExtension<MarkExtensionOptions, 'code', {
return { code: () => toggleMark(type) };
}

public inputRules({ type }: SchemaMarkTypeParams) {
public inputRules({ type }: ExtensionManagerMarkTypeParams) {
return [markInputRule({ regexp: /(?:`)([^`]+)(?:`)$/, type })];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
public pasteRules({ type }: ExtensionManagerMarkTypeParams) {
return [markPasteRule({ regexp: /(?:`)([^`]+)(?:`)/g, type })];
}
}
10 changes: 5 additions & 5 deletions @remirror/core-extensions/src/marks/italic-extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
CommandMarkTypeParams,
ExtensionManagerMarkTypeParams,
MarkExtension,
MarkExtensionOptions,
MarkExtensionSpec,
MarkGroup,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
} from '@remirror/core';
import { toggleMark } from 'prosemirror-commands';

export class ItalicExtension extends MarkExtension<MarkExtensionOptions, 'italic', {}> {
export class ItalicExtension extends MarkExtension<MarkExtensionOptions> {
get name() {
return 'italic' as const;
}
Expand All @@ -23,7 +23,7 @@ export class ItalicExtension extends MarkExtension<MarkExtensionOptions, 'italic
};
}

public keys({ type }: SchemaMarkTypeParams) {
public keys({ type }: ExtensionManagerMarkTypeParams) {
return {
'Mod-i': toggleMark(type),
};
Expand All @@ -33,11 +33,11 @@ export class ItalicExtension extends MarkExtension<MarkExtensionOptions, 'italic
return { italic: () => toggleMark(type) };
}

public inputRules({ type }: SchemaMarkTypeParams) {
public inputRules({ type }: ExtensionManagerMarkTypeParams) {
return [markInputRule({ regexp: /(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)$/, type })];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
public pasteRules({ type }: ExtensionManagerMarkTypeParams) {
return [markPasteRule({ regexp: /(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)/g, type })];
}
}
19 changes: 11 additions & 8 deletions @remirror/core-extensions/src/marks/link-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BooleanExtensionCheck,
Cast,
CommandMarkTypeParams,
ExtensionCommands,
ExtensionManagerMarkTypeParams,
getMarkRange,
getMatchString,
getSelectedWord,
Expand All @@ -16,14 +16,11 @@ import {
MarkGroup,
markPasteRule,
removeMark,
SchemaMarkTypeParams,
selectionEmpty,
updateMark,
} from '@remirror/core';
import { Plugin, TextSelection } from 'prosemirror-state';

export type InvokedFromType = 'keyboard' | 'input-rule';

export interface LinkExtensionOptions extends MarkExtensionOptions {
/**
* Return true to intercept the activation. This is useful for showing a dialog to replace the selected text.
Expand All @@ -33,7 +30,7 @@ export interface LinkExtensionOptions extends MarkExtensionOptions {

export type LinkExtensionCommands = 'updateLink' | 'removeLink';

export class LinkExtension extends MarkExtension<LinkExtensionOptions, LinkExtensionCommands, {}> {
export class LinkExtension extends MarkExtension<LinkExtensionOptions> {
get name() {
return 'link' as const;
}
Expand Down Expand Up @@ -97,14 +94,20 @@ export class LinkExtension extends MarkExtension<LinkExtensionOptions, LinkExten

public commands({ type }: CommandMarkTypeParams) {
return {
/**
* A command to update the selected link
*/
updateLink: (attrs?: Attrs) => updateMark({ type, attrs }),
/**
* Remove the link at the current position
*/
removeLink: () => {
return removeMark({ type, expand: true });
},
};
}

public isEnabled({ getState, type }: SchemaMarkTypeParams): BooleanExtensionCheck<ExtensionCommands<this>> {
public isEnabled({ getState, type }: ExtensionManagerMarkTypeParams): BooleanExtensionCheck {
return ({ command }) => {
switch (command) {
case 'removeLink':
Expand All @@ -121,7 +124,7 @@ export class LinkExtension extends MarkExtension<LinkExtensionOptions, LinkExten
};
}

public pasteRules({ type }: SchemaMarkTypeParams) {
public pasteRules({ type }: ExtensionManagerMarkTypeParams) {
return [
markPasteRule({
regexp: /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g,
Expand All @@ -131,7 +134,7 @@ export class LinkExtension extends MarkExtension<LinkExtensionOptions, LinkExten
];
}

public plugin({ type }: SchemaMarkTypeParams) {
public plugin({ type }: ExtensionManagerMarkTypeParams) {
return new Plugin({
props: {
handleClick(view, pos) {
Expand Down
10 changes: 5 additions & 5 deletions @remirror/core-extensions/src/marks/strike-extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
CommandMarkTypeParams,
ExtensionManagerMarkTypeParams,
MarkExtension,
MarkExtensionOptions,
MarkExtensionSpec,
MarkGroup,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
} from '@remirror/core';
import { toggleMark } from 'prosemirror-commands';

export class StrikeExtension extends MarkExtension<MarkExtensionOptions, 'strike'> {
export class StrikeExtension extends MarkExtension<MarkExtensionOptions> {
get name() {
return 'strike' as const;
}
Expand All @@ -37,7 +37,7 @@ export class StrikeExtension extends MarkExtension<MarkExtensionOptions, 'strike
};
}

public keys({ type }: SchemaMarkTypeParams) {
public keys({ type }: ExtensionManagerMarkTypeParams) {
return {
'Mod-d': toggleMark(type),
};
Expand All @@ -47,11 +47,11 @@ export class StrikeExtension extends MarkExtension<MarkExtensionOptions, 'strike
return { strike: () => toggleMark(type) };
}

public inputRules({ type }: SchemaMarkTypeParams) {
public inputRules({ type }: ExtensionManagerMarkTypeParams) {
return [markInputRule({ regexp: /~([^~]+)~$/, type })];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
public pasteRules({ type }: ExtensionManagerMarkTypeParams) {
return [markPasteRule({ regexp: /~([^~]+)~/g, type })];
}
}
6 changes: 3 additions & 3 deletions @remirror/core-extensions/src/marks/underline-extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
CommandMarkTypeParams,
ExtensionManagerMarkTypeParams,
MarkExtension,
MarkExtensionOptions,
MarkExtensionSpec,
MarkGroup,
SchemaMarkTypeParams,
} from '@remirror/core';
import { toggleMark } from 'prosemirror-commands';

export class UnderlineExtension extends MarkExtension<MarkExtensionOptions, 'underline', {}> {
export class UnderlineExtension extends MarkExtension<MarkExtensionOptions> {
get name() {
return 'underline' as const;
}
Expand All @@ -29,7 +29,7 @@ export class UnderlineExtension extends MarkExtension<MarkExtensionOptions, 'und
};
}

public keys({ type }: SchemaMarkTypeParams) {
public keys({ type }: ExtensionManagerMarkTypeParams) {
return {
'Mod-u': toggleMark(type),
};
Expand Down
8 changes: 4 additions & 4 deletions @remirror/core-extensions/src/nodes/blockquote-extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
CommandNodeTypeParams,
EDITOR_CLASS_SELECTOR,
ExtensionManagerNodeTypeParams,
NodeExtension,
NodeExtensionOptions,
NodeExtensionSpec,
NodeGroup,
SchemaNodeTypeParams,
toggleWrap,
} from '@remirror/core';
import { wrappingInputRule } from 'prosemirror-inputrules';

export class BlockquoteExtension extends NodeExtension<NodeExtensionOptions, 'blockquote', {}> {
export class BlockquoteExtension extends NodeExtension<NodeExtensionOptions> {
get name() {
return 'blockquote' as const;
}
Expand Down Expand Up @@ -45,13 +45,13 @@ export class BlockquoteExtension extends NodeExtension<NodeExtensionOptions, 'bl
`;
}

public keys({ type }: SchemaNodeTypeParams) {
public keys({ type }: ExtensionManagerNodeTypeParams) {
return {
'Ctrl->': toggleWrap(type),
};
}

public inputRules({ type }: SchemaNodeTypeParams) {
public inputRules({ type }: ExtensionManagerNodeTypeParams) {
return [wrappingInputRule(/^\s*>\s$/, type)];
}
}

0 comments on commit 876d7b6

Please sign in to comment.