Skip to content

Commit

Permalink
feat: new combined type
Browse files Browse the repository at this point in the history
Issue with using the manager after passing through a combined type
  • Loading branch information
ifiokjr committed May 28, 2020
1 parent 2203292 commit 1db2b2a
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 289 deletions.
4 changes: 2 additions & 2 deletions @remirror/core/src/builtins/attributes-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { bool, object } from '@remirror/core-helpers';
import { AttributesWithClass, Shape } from '@remirror/core-types';

import { AnyExtension, CreateLifecycleMethod, PlainExtension } from '../extension';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion } from '../preset';

/**
* This extension allows others extension to add the `createAttributes` method
Expand Down Expand Up @@ -84,7 +84,7 @@ export class AttributesExtension extends PlainExtension {

declare global {
namespace Remirror {
interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* The attributes to be added to the prosemirror editor.
*/
Expand Down
13 changes: 6 additions & 7 deletions @remirror/core/src/builtins/commands-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import {
ChainedFromExtensions,
CommandsFromExtensions,
CreateLifecycleMethod,
GetExtensionUnion,
PlainExtension,
ViewLifecycleMethod,
} from '../extension';
import { throwIfNameNotUnique } from '../helpers';
import { AnyPreset } from '../preset';
import { AnyPreset, ChainedFromCombined, CombinedUnion, CommandsFromCombined } from '../preset';
import { CommandShape, ExtensionCommandFunction, ExtensionCommandReturn } from '../types';

/**
Expand All @@ -41,7 +40,7 @@ export class CommandsExtension extends PlainExtension {
return 'commands' as const;
}

public onCreate: CreateLifecycleMethod = (extensions) => {
public onCreate: CreateLifecycleMethod = () => {
const { setExtensionStore, getStoreKey } = this.store;

setExtensionStore('getCommands', () => {
Expand Down Expand Up @@ -231,7 +230,7 @@ declare global {
namespace Remirror {
const _COMMANDS: unique symbol;

interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* Enables the use of custom commands created by the extensions for
* extending the functionality of your editor in an expressive way.
Expand Down Expand Up @@ -271,7 +270,7 @@ declare global {
* transaction.
*
*/
commands: CommandsFromExtensions<ExtensionUnion | GetExtensionUnion<PresetUnion>>;
commands: CommandsFromCombined<Combined>;

/**
* Chainable commands for composing functionality together in quaint and
Expand All @@ -292,7 +291,7 @@ declare global {
*
* The `run()` method ends the chain and dispatches the command.
*/
chain: ChainedFromExtensions<ExtensionUnion | GetExtensionUnion<PresetUnion>>;
chain: ChainedFromCombined<Combined>;
}

interface ExtensionCreatorMethods<
Expand Down Expand Up @@ -357,7 +356,7 @@ declare global {
: EmptyShape;
}

interface ExtensionStore<Schema extends EditorSchema = EditorSchema> {
interface ExtensionStore {
/**
* A method to return the editor's available commands.
*/
Expand Down
9 changes: 4 additions & 5 deletions @remirror/core/src/builtins/helpers-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { AnyFunction, EditorSchema, EmptyShape, Shape } from '@remirror/core-typ
import {
AnyExtension,
CreateLifecycleMethod,
GetExtensionUnion,
HelpersFromExtensions,
PlainExtension,
ViewLifecycleMethod,
} from '../extension';
import { throwIfNameNotUnique } from '../helpers';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion, HelpersFromCombined } from '../preset';
import { ExtensionHelperReturn } from '../types';

/**
Expand Down Expand Up @@ -74,11 +73,11 @@ declare global {
namespace Remirror {
const _HELPERS: unique symbol;

interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* The helpers provided by the extensions used.
*/
helpers: HelpersFromExtensions<ExtensionUnion | GetExtensionUnion<PresetUnion>>;
helpers: HelpersFromCombined<Combined>;
}

interface ExtensionCreatorMethods<
Expand Down Expand Up @@ -134,7 +133,7 @@ declare global {
: EmptyShape;
}

interface ExtensionStore<Schema extends EditorSchema = EditorSchema> {
interface ExtensionStore {
/**
* Helper method to provide information about the content of the editor.
* Each extension can register its own helpers.
Expand Down
4 changes: 2 additions & 2 deletions @remirror/core/src/builtins/node-views-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isFunction, object } from '@remirror/core-helpers';
import { NodeViewMethod, Shape } from '@remirror/core-types';

import { AnyExtension, CreateLifecycleMethod, PlainExtension } from '../extension';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion } from '../preset';

/**
* This extension allows others extension to add the `createNodeView` method
Expand Down Expand Up @@ -59,7 +59,7 @@ export class NodeViewsExtension extends PlainExtension {

declare global {
namespace Remirror {
interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* The custom nodeView which can be used to replace the nodes or marks in
* the dom and change their browser behaviour.
Expand Down
14 changes: 6 additions & 8 deletions @remirror/core/src/builtins/plugins-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import {
AnyExtension,
AnyExtensionConstructor,
CreateLifecycleMethod,
GetExtensionUnion,
PlainExtension,
} from '../extension';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion, InferCombinedExtensions } from '../preset';
import { GetNameUnion } from '../types';

/**
Expand Down Expand Up @@ -42,7 +41,7 @@ export class PluginsExtension extends PlainExtension {
/**
* Plugins created by the `createPlugin` methods and `createExternalPlugins`.
*/
private extensionPlugins: ProsemirrorPlugin[] = [];
private readonly extensionPlugins: ProsemirrorPlugin[] = [];

private readonly pluginKeys: Record<string, PluginKey> = object();

Expand Down Expand Up @@ -75,6 +74,7 @@ export class PluginsExtension extends PlainExtension {

// Assign the plugin key to the extension name.
this.pluginKeys[extension.name] = key;
// eslint-disable-next-line unicorn/consistent-function-scoping
const getter = <State>() => getPluginState<State>(key, getStoreKey('view').state);

extension.pluginKey = key;
Expand Down Expand Up @@ -191,7 +191,7 @@ export interface CreatePluginReturn<PluginState = any>

declare global {
namespace Remirror {
interface ExtensionStore<Schema extends EditorSchema = EditorSchema> {
interface ExtensionStore {
/**
* Retrieve the state for any given extension name. This will throw an
* error if the extension identified by that name doesn't implement the
Expand Down Expand Up @@ -242,7 +242,7 @@ declare global {
addPlugins: (...plugins: ProsemirrorPlugin[]) => void;
}

interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* All of the plugins combined together from all sources
*/
Expand All @@ -254,9 +254,7 @@ declare global {
*
* @param name - the name of the extension
*/
getPluginState: <State>(
name: GetNameUnion<ExtensionUnion | GetExtensionUnion<PresetUnion>>,
) => State;
getPluginState: <State>(name: GetNameUnion<InferCombinedExtensions<Combined>>) => State;

/**
* All the plugin keys available to be used by plugins.
Expand Down
21 changes: 7 additions & 14 deletions @remirror/core/src/builtins/schema-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import { AttributeSpec, ParseRule, Schema } from '@remirror/pm/model';
import {
AnyExtension,
CreateLifecycleMethod,
GetExtensionUnion,
GetMarkNameUnion,
GetNodeNameUnion,
isMarkExtension,
isNodeExtension,
PlainExtension,
SchemaFromExtensionUnion,
} from '../extension';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion, InferCombinedExtensions } from '../preset';

/**
* This extension creates the schema and provides extra attributes as defined in
Expand Down Expand Up @@ -276,30 +275,24 @@ declare global {
disableExtraAttributes?: boolean;
}

interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* The nodes to place on the schema.
*/
nodes: Record<
GetNodeNameUnion<ExtensionUnion | GetExtensionUnion<PresetUnion>>,
NodeExtensionSpec
>;
nodes: Record<GetNodeNameUnion<InferCombinedExtensions<Combined>>, NodeExtensionSpec>;

/**
* The marks to be added to the schema.
*/
marks: Record<
GetMarkNameUnion<ExtensionUnion | GetExtensionUnion<PresetUnion>>,
MarkExtensionSpec
>;
marks: Record<GetMarkNameUnion<InferCombinedExtensions<Combined>>, MarkExtensionSpec>;

/**
* The schema created by this extension manager.
*/
schema: SchemaFromExtensionUnion<ExtensionUnion | GetExtensionUnion<PresetUnion>>;
schema: SchemaFromExtensionUnion<InferCombinedExtensions<Combined>>;
}

interface ExtensionStore<Schema extends EditorSchema = EditorSchema> {
interface ExtensionStore {
/**
* The Prosemirror schema being used for the current editor.
*
Expand All @@ -311,7 +304,7 @@ declare global {
*
* Available: *return function* - `onCreate`
*/
schema: Schema;
schema: EditorSchema;
}
}
}
8 changes: 4 additions & 4 deletions @remirror/core/src/builtins/tags-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isNodeExtension,
PlainExtension,
} from '../extension';
import { AnyPreset } from '../preset';
import { AnyPreset, CombinedUnion, InferCombinedExtensions } from '../preset';
import { GeneralExtensionTags, MarkExtensionTags, NodeExtensionTags } from '../types';

/**
Expand Down Expand Up @@ -127,14 +127,14 @@ declare global {
tags?: Array<ExtensionTag | string>;
}

interface ManagerStore<ExtensionUnion extends AnyExtension, PresetUnion extends AnyPreset> {
interface ManagerStore<Combined extends CombinedUnion<AnyExtension, AnyPreset>> {
/**
* Store the built in and custom tags for the editor instance.
*/
tags: Readonly<ExtensionTags<ExtensionUnion>>;
tags: Readonly<ExtensionTags<InferCombinedExtensions<Combined>>>;
}

export interface ExtensionStore<Schema extends EditorSchema = EditorSchema> {
export interface ExtensionStore {
/**
* The tags provided by the configured extensions.
*/
Expand Down
49 changes: 34 additions & 15 deletions @remirror/core/src/editor-manager/editor-manager-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ import { ErrorConstant } from '@remirror/core-constants';
import { invariant, isEmptyArray, isFunction, object, sort } from '@remirror/core-helpers';

import { AnyExtension, AnyExtensionConstructor, isExtension } from '../extension';
import { AnyPreset, isPreset } from '../preset';
import {
AnyPreset,
CombinedUnion,
InferCombinedExtensions,
InferCombinedPresets,
isPreset,
} from '../preset';
import { GetConstructor } from '../types';

export interface TransformExtensionOrPreset<
ExtensionUnion extends AnyExtension,
PresetUnion extends AnyPreset
Combined extends CombinedUnion<AnyExtension, AnyPreset>
> {
extensions: ExtensionUnion[];
extensionMap: WeakMap<GetConstructor<ExtensionUnion>, ExtensionUnion>;
presets: PresetUnion[];
presetMap: WeakMap<GetConstructor<PresetUnion>, PresetUnion>;
extensions: Array<InferCombinedExtensions<Combined>>;
extensionMap: WeakMap<
GetConstructor<InferCombinedExtensions<Combined>>,
InferCombinedExtensions<Combined>
>;
presets: Array<InferCombinedPresets<Combined>>;
presetMap: WeakMap<
GetConstructor<InferCombinedPresets<Combined>>,
InferCombinedPresets<Combined>
>;
}

function isExtensionOfType<ExtensionUnion extends AnyExtension>(
value: unknown,
): value is ExtensionUnion {
return isExtension(value);
}
function isPresetOfType<PresetUnion extends AnyPreset>(value: unknown): value is PresetUnion {
return isPreset(value);
}

/**
Expand All @@ -28,13 +48,12 @@ export interface TransformExtensionOrPreset<
*
* @returns the list of extension instances sorted by priority
*/
export function transformExtensionOrPreset<
ExtensionUnion extends AnyExtension,
PresetUnion extends AnyPreset
>(
unionValues: ReadonlyArray<ExtensionUnion | PresetUnion>,
): TransformExtensionOrPreset<ExtensionUnion, PresetUnion> {
export function transformExtensionOrPreset<Combined extends CombinedUnion<AnyExtension, AnyPreset>>(
unionValues: readonly Combined[],
): TransformExtensionOrPreset<Combined> {
type ExtensionUnion = InferCombinedExtensions<Combined>;
type ExtensionConstructor = GetConstructor<ExtensionUnion>;
type PresetUnion = InferCombinedPresets<Combined>;
type PresetConstructor = GetConstructor<PresetUnion>;
interface MissingConstructor {
Constructor: AnyExtensionConstructor;
Expand Down Expand Up @@ -64,15 +83,15 @@ export function transformExtensionOrPreset<

for (const presetOrExtension of unionValues) {
// Update the extension list in this block
if (isExtension(presetOrExtension)) {
if (isExtensionOfType<ExtensionUnion>(presetOrExtension)) {
rawExtensions.push(presetOrExtension);
updateDuplicateMap(presetOrExtension);

continue;
}

// Update the presets list in this block
if (isPreset(presetOrExtension)) {
if (isPresetOfType<PresetUnion>(presetOrExtension)) {
presets.push(presetOrExtension);
rawExtensions.push(...(presetOrExtension.extensions as ExtensionUnion[]));
presetMap.set(presetOrExtension.constructor, presetOrExtension);
Expand Down

0 comments on commit 1db2b2a

Please sign in to comment.