Skip to content

Commit

Permalink
refactor: continue with the refactor back to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed May 20, 2020
1 parent 8c1c503 commit 99d7650
Show file tree
Hide file tree
Showing 30 changed files with 512 additions and 378 deletions.
2 changes: 1 addition & 1 deletion @remirror/core/src/builtins/attributes-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { AnyPreset } from '../preset';
*/
export class AttributesExtension extends PlainExtension {
public readonly name = 'attributes' as const;
public readonly defaultPriority = ExtensionPriority.High;
public readonly defaultPriority = ExtensionPriority.High as const;

protected createDefaultSettings() {
return {};
Expand Down
52 changes: 17 additions & 35 deletions @remirror/core/src/builtins/builtin-preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PresetFactory } from '../preset';
import { Of } from '../types';
import { Preset } from '../preset';
import { AttributesExtension } from './attributes-extension';
import { CommandsExtension } from './commands-extension';
import { HelpersExtension } from './helpers-extension';
Expand Down Expand Up @@ -39,39 +38,22 @@ export const builtInExtensions = [
*
* @builtin
*/
export const BuiltinPreset = PresetFactory.typed().preset({
name: 'builtin',
createExtensions() {
return builtInExtensions.map((extension) => extension.of());
},
});
export class BuiltinPreset extends Preset {
public name = 'builtin' as const;

/*
const BuiltinPreset = PresetFactory.typed<
{ excludeStrikeThrough: boolean },
{ boldWeight: number }
>().preset({
name: 'builtin',
defaultSettings: {
excludeStrikeThrough: false,
},
defaultProperties: {
boldWeight: 500,
},
createExtensions({ settings }) {
if (settings.excludeStrikeThrough) {
}
return builtInExtensions.map(extension => extension.of());
},
onSetProperties({ getExtension, changes, previous, update }) {
if (changes.boldWeight.changed) {
protected createDefaultSettings() {
return {};
}

const extension = getExtension(InputRulesExtension);
extension.setProperties({weight: changes.boldWeight.value})
}
},
});
*/
protected createDefaultProperties(): Required<{}> {
return {};
}

/** The built in extension as a type. */
export type BuiltInExtensions = Of<typeof builtInExtensions[number]>;
protected onSetProperties(): void {
return;
}

public createExtensions() {
return builtInExtensions.map((Extension) => new Extension());
}
}
43 changes: 20 additions & 23 deletions @remirror/core/src/builtins/input-rules-extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ExtensionPriority } from '@remirror/core-constants';
import { And, Shape } from '@remirror/core-types';
import { Shape } from '@remirror/core-types';
import { InputRule, inputRules } from '@remirror/pm/inputrules';

import { Extension, ExtensionFactory } from '../extension';
import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } from '../types';
import { InitializeLifecycleMethod, PlainExtension } from '../extension';

/**
* This extension allows others extension to add the `createInputRules` method
Expand All @@ -16,14 +15,23 @@ import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } f
*
* @builtin
*/
export const InputRulesExtension = ExtensionFactory.plain({
name: 'inputRules',
defaultPriority: ExtensionPriority.High,
export class InputRulesExtension extends PlainExtension {
public readonly name = 'inputRules' as const;
public readonly defaultPriority = ExtensionPriority.High as const;

protected createDefaultSettings() {
return {};
}

protected createDefaultProperties() {
return {};
}

/**
* Ensure that all ssr transformers are run.
*/
onInitialize({ getParameter, addPlugins, managerSettings }) {
public onInitialize: InitializeLifecycleMethod = (parameter) => {
const { addPlugins, managerSettings } = parameter;
const rules: InputRule[] = [];

return {
Expand All @@ -32,23 +40,22 @@ export const InputRulesExtension = ExtensionFactory.plain({
// managerSettings excluded this from running
managerSettings.exclude?.inputRules ||
// Method doesn't exist
!extension.parameter.createInputRules ||
!extension.createInputRules ||
// Extension settings exclude it
extension.settings.exclude.inputRules
) {
return;
}

const parameter = getParameter(extension);
rules.push(...extension.parameter.createInputRules(parameter));
rules.push(...extension.createInputRules());
},

afterExtensionLoop: () => {
addPlugins(inputRules({ rules }));
},
};
},
});
};
}

declare global {
namespace Remirror {
Expand All @@ -68,17 +75,7 @@ declare global {
*
* @param parameter - schema parameter with type included
*/
createInputRules?: (
parameter: And<
ManagerTypeParameter<ProsemirrorType>,
{
/**
* The extension which provides access to the settings and properties.
*/
extension: Extension<Name, Settings, Properties, Commands, Helpers, ProsemirrorType>;
}
>,
) => InputRule[];
createInputRules?: () => InputRule[];
}
}
}
42 changes: 20 additions & 22 deletions @remirror/core/src/builtins/keymap-extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ExtensionPriority } from '@remirror/core-constants';
import { hasOwnProperty, object } from '@remirror/core-helpers';
import {
And,
KeyBindingCommandFunction,
KeyBindings,
ProsemirrorCommandFunction,
Expand All @@ -10,8 +9,7 @@ import {
import { chainKeyBindingCommands } from '@remirror/core-utils';
import { keymap } from '@remirror/pm/keymap';

import { Extension, ExtensionFactory } from '../extension';
import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } from '../types';
import { InitializeLifecycleMethod, PlainExtension } from '../extension';

/**
* This extension allows others extension to use the `createKeymaps` method.
Expand All @@ -23,13 +21,23 @@ import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } f
*
* @builtin
*/
export const KeymapExtension = ExtensionFactory.plain({
name: 'keymap',
defaultPriority: ExtensionPriority.High,
export class KeymapExtension extends PlainExtension {
public readonly name = 'keymap' as const;
public readonly defaultPriority = ExtensionPriority.High as const;

protected createDefaultSettings(): import('../extension').DefaultSettingsType<{}> {
return {};
}

protected createDefaultProperties(): Required<{}> {
return {};
}

/**
* This adds the `createKeymap` method functionality to all extensions.
*/
onInitialize({ getParameter, addPlugins, managerSettings }) {
public onInitialize: InitializeLifecycleMethod = (parameter) => {
const { addPlugins, managerSettings } = parameter;
const extensionKeymaps: KeyBindings[] = [];

return {
Expand All @@ -39,14 +47,14 @@ export const KeymapExtension = ExtensionFactory.plain({
// The user doesn't want any keymaps in the editor.
managerSettings.exclude?.keymap ||
// The extension doesn't have the `createKeymap` method.
!extension.parameter.createKeymap ||
!extension.createKeymap ||
// The extension was configured to ignore the keymap.
extension.settings.exclude.keymap
) {
return;
}

extensionKeymaps.push(extension.parameter.createKeymap(getParameter(extension)));
extensionKeymaps.push(extension.createKeymap());
},
afterExtensionLoop: () => {
const previousCommandsMap = new Map<string, KeyBindingCommandFunction[]>();
Expand All @@ -73,8 +81,8 @@ export const KeymapExtension = ExtensionFactory.plain({
addPlugins(keymap(mappedCommands));
},
};
},
});
};
}

declare global {
namespace Remirror {
Expand All @@ -93,17 +101,7 @@ declare global {
*
* @param parameter - schema parameter with type included
*/
createKeymap?: (
parameter: And<
ManagerTypeParameter<ProsemirrorType>,
{
/**
* The extension which provides access to the settings and properties.
*/
extension: Extension<Name, Settings, Properties, Commands, Helpers, ProsemirrorType>;
}
>,
) => KeyBindings;
createKeymap?: () => KeyBindings;
}
}
}
44 changes: 21 additions & 23 deletions @remirror/core/src/builtins/node-views-extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ExtensionPriority } from '@remirror/core-constants';
import { isFunction, object } from '@remirror/core-helpers';
import { And, NodeViewMethod, Shape } from '@remirror/core-types';
import { NodeViewMethod, Shape } from '@remirror/core-types';

import { AnyExtension, Extension, ExtensionFactory } from '../extension';
import { AnyExtension, InitializeLifecycleMethod, PlainExtension } from '../extension';
import { AnyPreset } from '../preset';
import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } from '../types';

/**
* This extension allows others extension to add the `createNodeView` method
Expand All @@ -17,14 +16,24 @@ import { ExtensionCommandReturn, ExtensionHelperReturn, ManagerTypeParameter } f
*
* @builtin
*/
export const NodeViewsExtension = ExtensionFactory.plain({
name: 'nodeView',
defaultPriority: ExtensionPriority.High,
export class NodeViewsExtension extends PlainExtension {
public readonly name = 'nodeView' as const;
public readonly defaultPriority = ExtensionPriority.High as const;

protected createDefaultSettings(): import('../extension').DefaultSettingsType<{}> {
return {};
}

protected createDefaultProperties(): Required<{}> {
return {};
}

/**
* Ensure that all ssr transformers are run.
*/
onInitialize({ getParameter, setStoreKey, managerSettings }) {
public onInitialize: InitializeLifecycleMethod = (parameter) => {
const { setStoreKey, managerSettings } = parameter;

const nodeViewList: Array<Record<string, NodeViewMethod>> = [];
const nodeViews: Record<string, NodeViewMethod> = object();

Expand All @@ -34,15 +43,14 @@ export const NodeViewsExtension = ExtensionFactory.plain({
// managerSettings excluded this from running
managerSettings.exclude?.nodeViews ||
// Method doesn't exist
!extension.parameter.createNodeViews ||
!extension.createNodeViews ||
// Extension settings exclude it
extension.settings.exclude.nodeViews
) {
return;
}

const parameter = getParameter(extension);
const nodeView = extension.parameter.createNodeViews(parameter);
const nodeView = extension.createNodeViews();

// Unshift used to add make sure higher priority extensions can
// overwrite the lower priority nodeViews.
Expand All @@ -57,8 +65,8 @@ export const NodeViewsExtension = ExtensionFactory.plain({
setStoreKey('nodeViews', nodeViews);
},
};
},
});
};
}

declare global {
namespace Remirror {
Expand Down Expand Up @@ -94,17 +102,7 @@ declare global {
*
* @alpha
*/
createNodeViews?: (
parameter: And<
ManagerTypeParameter<ProsemirrorType>,
{
/**
* The extension which provides access to the settings and properties.
*/
extension: Extension<Name, Settings, Properties, Commands, Helpers, ProsemirrorType>;
}
>,
) => NodeViewMethod | Record<string, NodeViewMethod>;
createNodeViews?: () => NodeViewMethod | Record<string, NodeViewMethod>;
}
}
}

0 comments on commit 99d7650

Please sign in to comment.