Skip to content

Commit

Permalink
feat: improve extraAttributes in core
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Jun 1, 2020
1 parent 700857d commit 65c4ebe
Show file tree
Hide file tree
Showing 26 changed files with 310 additions and 288 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const tsProjectRules = {
'@typescript-eslint/restrict-plus-operands': 'warn',
'@typescript-eslint/no-misused-promises': 'warn',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-nullish-coalescing': [
'error',
{ ignoreConditionalTests: true, ignoreMixedLogicalExpressions: true },
],
};

const schemaJsonFilePath = `${__dirname}/docs/.cache/caches/gatsby-plugin-typegen/schema.json`;
Expand Down
3 changes: 3 additions & 0 deletions @remirror/core-constants/src/error-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export enum ErrorConstant {
/** An error occurred within an extension. */
EXTENSION = 'RMR0100',

/** The spec was defined without calling the `defaults`, `parse` or `dom` methods. */
EXTENSION_SPEC = 'RMR0101',

/**
* `useRemirror` was called outside of the remirror context. It can only be used
* within an active remirror context created by the `<RemirrorProvider />`.
Expand Down
4 changes: 3 additions & 1 deletion @remirror/core-helpers/src/core-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ if (process.env.NODE_ENV !== 'production') {
[ErrorConstant.INVALID_NAME]: 'An invalid name was used for the extension.',
[ErrorConstant.EXTENSION]:
'An error occurred within an extension. More details should be made available.',
[ErrorConstant.EXTENSION_SPEC]:
'The spec was defined without calling the `defaults`, `parse` or `dom` methods.',
[ErrorConstant.REACT_PROVIDER_CONTEXT]:
'`useRemirror` was called outside of the `remirror` context. It can only be used within an active remirror context created by the `<RemirrorProvider />`.',
[ErrorConstant.REACT_GET_ROOT_PROPS]:
Expand All @@ -71,7 +73,7 @@ function isErrorConstant(code: unknown): code is ErrorConstant {
function createErrorMessage(code: ErrorConstant, extraMessage?: string) {
const message = errorMessageMap[code];
const prefix = message ? `${message}\n\n` : '';
const customMessage = extraMessage ? `${extraMessage}\n` : '';
const customMessage = extraMessage ? `${extraMessage}\n\n` : '';

return `${prefix}${customMessage}For more information visit ${ERROR_INFORMATION_URL}#${code}`;
}
Expand Down
49 changes: 32 additions & 17 deletions @remirror/core-types/src/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ConditionalExcept, ConditionalPick } from 'type-fest';

import { AttributeSpec } from '@remirror/pm/model';

import { ProsemirrorNode } from './alias-types';

/**
* An alternative to keyof that only extracts the string keys.
*/
Expand Down Expand Up @@ -217,36 +219,49 @@ export type ProsemirrorAttributes<Extra extends object = object> = Record<string
export type AttributesWithClass = ProsemirrorAttributes & { class?: string };

export interface ExtraAttributesObject {
/**
* The name of the attribute
*/
name: string;

/**
* The default value for the attr, if left undefined then this becomes a
* required. and must be provided whenever a node or mark of a type that has
* them is created.
*/
default?: string | null;
default: string | null;

/**
* A function used to extract the attribute from the dom.
* A function used to extract the attribute from the dom and must be applied
* to the `parseDOM` method.
*/
getAttribute?: (domNode: Node) => unknown;
parseDOM?: (domNode: HTMLElement) => unknown;

/**
* Takes the node attributes and applies them to the dom.
*
* This is called in the `toDOM` method.
*/
toDOM?: (attrs: ProsemirrorAttributes) => string;
}

/**
* The first value is the name of the attribute the second value is the default
* and the third is the optional parse name from the dom via
* `node.getAttribute()`.
*/
export type ExtraAttributesTuple = [string, string, string?];
export interface ApplyExtraAttributes {
/**
* A function which returns the object of defaults. Since this is for extra
* attributes a default must be provided.
*/
defaults: () => Record<string, { default: string | null }>;

/**
* Transform the dom to prosemirror attributes
*/
parse: (domNode: Node | string) => ProsemirrorAttributes;

/**
* Take the node attributes and create an object that can be stored in the dom.
*/
dom: (node: ProsemirrorAttributes) => Record<string, string>;
}

/**
* Data representation tuple used for injecting extra attributes into an
* extension.
* A mapping of the attribute name to it's default, getter and setter.
*/
export type ExtraAttributes = string | ExtraAttributesTuple | ExtraAttributesObject;
export type ExtraAttributes = Record<string, ExtraAttributesObject>;

/**
* A method that can pull all the extraAttributes from the provided dom node.
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/attributes-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ declare global {
attributes?: boolean;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Allows the extension to modify the attributes for the Prosemirror editor
* dom element.
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/commands-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,7 @@ declare global {
chain: ChainedFromCombined<Combined>;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Create and register commands for that can be called within the editor.
*
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/helpers-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ declare global {
helpers: HelpersFromCombined<Combined>;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* A helper method is a function that takes in arguments and returns a
* value depicting the state of the editor specific to this extension.
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/input-rules-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ declare global {
inputRules?: boolean;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Register input rules which are activated if the regex matches as a user is
* typing.
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/keymap-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ declare global {
rebuildKeymap: () => void;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Add keymap bindings for this extension.
*
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/node-views-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ declare global {
nodeViews?: boolean;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Registers one or multiple nodeViews for the extension.
*
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/paste-rules-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ declare global {
pasteRules?: boolean;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Register paste rules for this extension.
*
Expand Down
5 changes: 1 addition & 4 deletions @remirror/core/src/builtins/plugins-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ declare global {
plugin: Plugin;
}

interface ExtensionCreatorMethods<
Settings extends Shape = object,
Properties extends Shape = object
> {
interface ExtensionCreatorMethods {
/**
* Create a custom plugin directly in the editor.
*
Expand Down

0 comments on commit 65c4ebe

Please sign in to comment.