Skip to content

Commit

Permalink
feat: add package @remirror/dom
Browse files Browse the repository at this point in the history
Also multiple updates to the editor-wrapper interface.
  • Loading branch information
ifiokjr committed Jun 17, 2020
1 parent 8330cce commit 10b2cf4
Show file tree
Hide file tree
Showing 25 changed files with 1,461 additions and 796 deletions.
14 changes: 0 additions & 14 deletions @remirror/core-types/src/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,6 @@ export interface ObjectMark {
attrs?: Record<string, string | null>;
}

/**
* A JSON representation of the prosemirror Node.
*
* @remarks
* This is used to represent the top level doc nodes content.
*/
export interface RemirrorJSON {
type: string;
marks?: Array<ObjectMark | string>;
text?: string;
content?: RemirrorJSON[];
attrs?: Record<string, Literal | object>;
}

/**
* Defines a position
*/
Expand Down
34 changes: 33 additions & 1 deletion @remirror/core-types/src/core-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,37 @@ import {
EditorView,
Mark,
ProsemirrorNode,
Selection,
Transaction,
} from './alias-types';
import { ProsemirrorAttributes, RegexTuple, RemirrorJSON } from './base-types';
import { Literal, ObjectMark, ProsemirrorAttributes, RegexTuple } from './base-types';
import {
EditorStateParameter,
EditorViewParameter,
FromToParameter,
TransactionParameter,
} from './parameter-builders';

/**
* A JSON representation of the prosemirror Node.
*
* @remarks
* This is used to represent the top level doc nodes content.
*/
export interface RemirrorJSON {
type: string;
marks?: Array<ObjectMark | string>;
text?: string;
content?: RemirrorJSON[];
attrs?: Record<string, Literal | object>;
}

export interface StateJSON {
[key: string]: unknown;
doc: RemirrorJSON;
selection: FromToParameter;
}

type GetAttributesFunction = (p: string[] | string) => ProsemirrorAttributes | undefined;

/**
Expand Down Expand Up @@ -316,3 +338,13 @@ export type NodeViewMethod<View extends NodeView = NodeView> = (
export interface RemirrorIdentifierShape {
[__INTERNAL_REMIRROR_IDENTIFIER_KEY__]: RemirrorIdentifier;
}

/**
* The type of arguments acceptable for a selection.
*
* - Can be a selection
* - A range of `{ from: number; to: number }`
* - A single position with a `number`
* - `'start' | 'end'`
*/
export type PrimitiveSelection = Selection | FromToParameter | number | 'start' | 'end';
4 changes: 2 additions & 2 deletions @remirror/core-utils/src/dom-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ export interface CreateDocumentNodeParameter
selection?: FromToParameter;
}

export type CreateDocumentErrorHandler = (error: Error) => Fallback;
export type CreateDocumentErrorHandler = (error?: Error) => Fallback;
export type Fallback = RemirrorJSON | ProsemirrorNode;
export interface StringHandlerParameter {
/**
Expand All @@ -795,7 +795,7 @@ export interface StringHandlerParameter {
interface CreateDocumentErrorHandlerParameter {
onError: Fallback | CreateDocumentErrorHandler;
schema: EditorSchema;
error: Error;
error?: Error;
}

function createDocumentErrorHandler(parameter: CreateDocumentErrorHandlerParameter) {
Expand Down
1 change: 1 addition & 0 deletions @remirror/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@remirror/core-types": "^0.9.0",
"@remirror/core-utils": "^0.8.0",
"linaria": "^2.0.0-alpha.5",
"nanoevents": "^5.1.8",
"semver": "^7.3.2",
"type-fest": "^0.15.1"
},
Expand Down
23 changes: 18 additions & 5 deletions @remirror/core/src/builtins/commands-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,23 @@ export class CommandsExtension extends PlainExtension {
* This is primarily intended for use within a chainable command chain.
*/
customTransaction(transactionUpdater: (transaction: Transaction) => void): CommandFunction {
return ({ state, dispatch }) => {
return ({ tr, dispatch }) => {
if (dispatch) {
transactionUpdater(state.tr);
dispatch(state.tr);
transactionUpdater(tr);
dispatch(tr);
}

return true;
};
},

/**
* Insert text into the dom at the current location.
*/
insertText(text: string): CommandFunction {
return ({ tr, dispatch }) => {
if (dispatch) {
dispatch(tr.insertText(text));
}

return true;
Expand All @@ -166,9 +179,9 @@ export class CommandsExtension extends PlainExtension {
* plugin state have updated.
*/
emptyUpdate(): CommandFunction {
return ({ state, dispatch }) => {
return ({ tr, dispatch }) => {
if (dispatch) {
dispatch(state.tr);
dispatch(tr);
}

return true;
Expand Down
44 changes: 29 additions & 15 deletions @remirror/core/src/editor-manager/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
create: CreateLifecycleMethod[];
view: ViewLifecycleMethod[];
ready: Array<() => void>;
transaction: StateUpdateLifecycleMethod[];
update: StateUpdateLifecycleMethod[];
destroy: DestroyLifecycleMethod[];
} = {
create: [],
view: [],
ready: [],
transaction: [],
update: [],
destroy: [],
};

Expand Down Expand Up @@ -311,11 +311,11 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
}

if (readyHandler) {
this.#handlers.view.push(readyHandler);
this.#handlers.ready.push(readyHandler);
}

if (transactionHandler) {
this.#handlers.transaction.push(transactionHandler);
this.#handlers.update.push(transactionHandler);
}

if (destroyHandler) {
Expand Down Expand Up @@ -346,7 +346,8 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
};

/**
* A method to set values in the extension store which is made available to extension.
* A method to set values in the extension store which is made available to
* extension.
*
* **NOTE** This method should only be used in the `onCreate` extension method
* or it will throw an error.
Expand Down Expand Up @@ -435,6 +436,11 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
* @param view - the editor view
*/
addView(view: EditorView<this['~Sch']>) {
invariant(this.#phase < ManagerPhase.EditorView, {
code: ErrorConstant.MANAGER_PHASE_ERROR,
message: 'A view has already been added to this manager. A view should only be added once.',
});

// Update the lifecycle phase.
this.#phase = ManagerPhase.EditorView;

Expand All @@ -449,7 +455,11 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
}

/**
* This method should be called by the view layer to notify remirror that everything is ready to run.
* This method should be called by the view layer to notify remirror that
* everything is ready to run.
*
* This is useful in frameworks like `react` where the view is only attached
* to the dom once the component is mounted.
*/
ready() {
this.#phase = ManagerPhase.Ready;
Expand Down Expand Up @@ -542,16 +552,19 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
* An example usage of this is within the collaboration plugin.
*/
onStateUpdate(parameter: StateUpdateLifecycleParameter) {
this.#phase = ManagerPhase.Runtime;

this.#extensionStore.currentState = parameter.state;
this.#extensionStore.previousState = parameter.previousState;

for (const handler of this.#handlers.transaction) {
for (const handler of this.#handlers.update) {
handler(parameter);
}
}

/**
* Get the extension instance matching the provided constructor from the manager.
* Get the extension instance matching the provided constructor from the
* manager.
*
* This will throw an error if non existent.
*/
Expand Down Expand Up @@ -588,7 +601,8 @@ export class EditorManager<Combined extends AnyCombinedUnion> {
/**
* Recreate the manager.
*
* What about the state stored in the extensions and presets these need to be recreated as well
* What about the state stored in the extensions and presets these need to be
* recreated as well
*/
clone<ExtraCombined extends AnyCombinedUnion>(
combined: ExtraCombined[],
Expand Down Expand Up @@ -652,8 +666,8 @@ export interface EditorManagerParameter<

interface SettingsWithPrivacy extends Remirror.ManagerSettings {
/**
* A symbol value that prevents the EditorManager constructor from being called
* directly.
* A symbol value that prevents the EditorManager constructor from being
* called directly.
*
* @internal
*/
Expand Down Expand Up @@ -698,8 +712,8 @@ export interface EditorManager<Combined extends AnyCombinedUnion> {
/**
* `NodeNames`
*
* Type inference hack for node extension names.
* This is the only way I know to store types on a class.
* Type inference hack for node extension names. This is the only way I know
* to store types on a class.
*
* @internal
*/
Expand All @@ -708,8 +722,8 @@ export interface EditorManager<Combined extends AnyCombinedUnion> {
/**
* `MarkNames`
*
* Type inference hack for mark extension names.
* This is the only way I know to store types on a class.
* Type inference hack for mark extension names. This is the only way I know
* to store types on a class.
*
* @internal
*/
Expand Down

0 comments on commit 10b2cf4

Please sign in to comment.