Skip to content

Commit

Permalink
feat: rewrite emoji extension
Browse files Browse the repository at this point in the history
Type inference is messed up though.
  • Loading branch information
ifiokjr committed May 18, 2020
1 parent 9af0d3b commit 91e668b
Show file tree
Hide file tree
Showing 29 changed files with 458 additions and 320 deletions.
40 changes: 35 additions & 5 deletions @remirror/core/src/builtins/commands-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface UnchainedFactoryParameter {
function unchainedFactory(parameter: UnchainedFactoryParameter) {
return (...args: unknown[]) => {
const { shouldDispatch = true, view, command, getState } = parameter;
let dispatch: DispatchFunction | undefined = undefined;
let dispatch: DispatchFunction | undefined;

if (shouldDispatch) {
dispatch = view.dispatch;
Expand Down Expand Up @@ -114,11 +114,18 @@ export const CommandsExtension = ExtensionFactory.plain({
beforeExtensionLoop() {
const { setManagerMethodParameter, getStoreKey } = parameter;

setManagerMethodParameter('commands', () => {
setManagerMethodParameter('getCommands', () => {
const commands = getStoreKey('commands');
invariant(commands, { code: ErrorConstant.COMMANDS_CALLED_IN_OUTER_SCOPE });

return commands.chain;
return commands as any;
});

setManagerMethodParameter('getChain', () => {
const chain = getStoreKey('chain');
invariant(chain, { code: ErrorConstant.COMMANDS_CALLED_IN_OUTER_SCOPE });

return chain as any;
});
},
};
Expand Down Expand Up @@ -175,9 +182,9 @@ export const CommandsExtension = ExtensionFactory.plain({
}

chained.run = () => view.dispatch(view.state.tr);
commands.chain = chained;

setStoreKey('commands', commands);
setStoreKey('chain', chained);
},
};
},
Expand Down Expand Up @@ -250,6 +257,21 @@ declare global {
*
*/
commands: CommandsFromExtensions<ExtensionUnion | GetExtensionUnion<PresetUnion>>;

/**
* Chainable commands for composing functionality together in quaint and
* beautiful ways...
*
* @remarks
*
* You can use this property to create expressive and complex commands that
* build up the transaction until it can be run.
*
* ```ts
* chain.bold().insertText('Hi').setSelection('start').run();
* ```
*/
chain: ChainedFromExtensions<ExtensionUnion | GetExtensionUnion<PresetUnion>>;
}

interface ExtensionCreatorMethods<
Expand Down Expand Up @@ -321,6 +343,14 @@ declare global {
}

interface ManagerMethodParameter<Schema extends EditorSchema = EditorSchema> {
/**
* A method to return the editor's available commands.
*/
getCommands: <ExtensionUnion extends AnyExtension = any>() => CommandsFromExtensions<
Of<typeof CommandsExtension>
> &
CommandsFromExtensions<ExtensionUnion>;

/**
* A method that returns an object with all the chainable commands
* available to be run.
Expand Down Expand Up @@ -353,7 +383,7 @@ declare global {
* })
* ```
*/
commands: <ExtensionUnion extends AnyExtension = any>() => ChainedFromExtensions<
getChain: <ExtensionUnion extends AnyExtension = any>() => ChainedFromExtensions<
Of<typeof CommandsExtension>
> &
ChainedFromExtensions<ExtensionUnion>;
Expand Down
7 changes: 2 additions & 5 deletions @remirror/core/src/builtins/plugins-extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExtensionPriority } from '@remirror/core-constants';
import { invariant, object } from '@remirror/core-helpers';
import { And, EditorSchema, ProsemirrorPlugin } from '@remirror/core-types';
import { EditorSchema, ProsemirrorPlugin } from '@remirror/core-types';
import { getPluginState } from '@remirror/core-utils';
import { Plugin, PluginKey } from '@remirror/pm/state';

Expand Down Expand Up @@ -38,10 +38,7 @@ export const PluginsExtension = ExtensionFactory.plain({
// These methods are later used.
onCreate({ setStoreKey, setManagerMethodParameter, getStoreKey }) {
const pluginKeys: Record<string, PluginKey> = object();
const stateGetters: Map<
string | AnyExtensionConstructor,
<State = unknown>() => State
> = object();
const stateGetters = new Map<string | AnyExtensionConstructor, <State = unknown>() => State>();

// Method for retrieving the plugin state by the extension name.
const getStateByName = <State>(identifier: string | AnyExtensionConstructor) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DocExtension } from '@remirror/extension-doc';
import { ParagraphExtension } from '@remirror/extension-paragraph';
import { CorePreset } from '@remirror/preset-core';
import { CorePreset, DocExtension, ParagraphExtension } from '@remirror/test-fixtures';

import { EditorManager } from '../editor-manager';

EditorManager.of([ParagraphExtension.of(), DocExtension.of(), CorePreset.of()]);
EditorManager.of({
extensions: [ParagraphExtension.of(), DocExtension.of()],
presets: [CorePreset.of()],
});
19 changes: 3 additions & 16 deletions @remirror/core/src/editor-manager/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import {
AnyExtension,
AnyExtensionConstructor,
AnyManagerStore,
CommandsFromExtensions,
CreateLifecycleMethodParameter,
CreateLifecycleMethodReturn,
GetExtensionUnion,
HelpersFromExtensions,
InitializeLifecycleMethodParameter,
InitializeLifecycleMethodReturn,
ManagerStoreKeys,
Expand All @@ -36,6 +34,7 @@ import {
import { AnyPreset } from '../preset';
import {
GetConstructor,
GetExtensions,
Of,
TransactionHandlerParameter,
TransactionHandlerReturn,
Expand All @@ -47,7 +46,7 @@ import {
transformExtensionOrPreset as transformExtensionOrPresetList,
} from './editor-manager-helpers';

export type AnyEditorManager = EditorManager<AnyExtension, AnyPreset>;
export type AnyEditorManager = EditorManager<any, any>;

/**
* Checks to see whether the provided value is an `Manager`.
Expand Down Expand Up @@ -141,26 +140,14 @@ export class EditorManager<ExtensionUnion extends AnyExtension, PresetUnion exte
/**
* Pseudo property which is a small hack to store the type of the extension union.
*/
public ['~E']!: ExtensionUnion | GetExtensionUnion<PresetUnion>;
public ['~E']!: ExtensionUnion | GetExtensions<PresetUnion>;

/**
* Pseudo property which is a small hack to store the type of the presets
* available from this manager..
*/
public ['~P']!: PresetUnion;

/**
* Pseudo property which is a small hack to store the type of the commands
* available from this manager.
*/
public ['~C']!: CommandsFromExtensions<this['~E']>;

/**
* Pseudo property which is a small hack to store the type of the helpers
* available from this manager.
*/
public ['~H']!: HelpersFromExtensions<this['~E']>;

/**
* Pseudo property which is a small hack to store the type of the schema
* available from this manager..
Expand Down
46 changes: 40 additions & 6 deletions @remirror/core/src/extension/extension-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ export abstract class Extension<
*/
#properties: Required<Properties>;

/**
* An internal data store for this extension. Can be used to store internal data.
*/
#data: any;

/**
* The parameter that was passed when creating the constructor for this instance.
*/
Expand Down Expand Up @@ -409,6 +414,11 @@ export abstract class Extension<
});

this.#properties = { ...this.defaultProperties, ...settings?.properties };

this.#data = this.parameter.setInitialData?.({
settings: this.#settings,
properties: this.#properties,
});
}

/**
Expand Down Expand Up @@ -448,6 +458,24 @@ export abstract class Extension<
this.parameter.onDestroy?.();
}

/**
* Get the internal data from the extension.
*
* Used to store persistent internal state.
*/
public getData<Data>(): Data {
return this.#data;
}

/**
* Set the internal data for the extension.
*
* Used to store persistent internal state.
*/
public setData<Data>(data: Data) {
this.#data = data;
}

/**
* Override the default toString method to match the native toString methods.
*/
Expand Down Expand Up @@ -509,13 +537,7 @@ interface DefaultSettingsParameter<Settings extends object> {
* supported for partial settings at this point in time. As a workaround
* use `null` as the type and pass it as the value in the default settings.
*/
/**
* The default settings for instances of this extension.
*/
defaultSettings: DefaultSettingsType<Settings>;

/**
* The default properties for instances of this extension. */
}

/**
Expand Down Expand Up @@ -597,6 +619,18 @@ export interface BaseExtensionFactoryParameter<
* thrown.
*/
requiredExtensions?: readonly AnyExtensionConstructor[];

/**
* Set the initial private data.
*
* Data is a way of storing internal state for the extension.
*/
setInitialData?: (parameter: ExtensionSettingsAndProperties<Settings, Properties>) => unknown;
}

interface ExtensionSettingsAndProperties<Settings extends object, Properties extends object> {
settings: Readonly<Required<Settings & BaseExtensionSettings>>;
properties: Required<Properties>;
}

interface ExtensionCreatorMethods<
Expand Down
17 changes: 1 addition & 16 deletions @remirror/core/src/extension/extension-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,7 @@ type MapToChainedCommand<RawCommands extends Record<string, AnyFunction>> = {
*/
export type CommandsFromExtensions<ExtensionUnion extends AnyExtension> = UnionToIntersection<
MapToUnchainedCommand<GetCommands<ExtensionUnion>>
> & {
/**
* Chainable commands for composing functionality together in quaint and
* beautiful ways...
*
* @remarks
*
* You can use this property to create expressive and complex commands that
* build up the transaction until it can be run.
*
* ```ts
* commands.chain.bold().insertText('Hi').setSelection('start').run();
* ```
*/
chain: ChainedFromExtensions<ExtensionUnion>;
};
>;

export interface ChainedCommandRunParameter {
/**
Expand Down
6 changes: 6 additions & 0 deletions @remirror/core/src/preset/preset-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ export abstract class Preset<
*/
public readonly ['~P']!: Properties;

/**
* Not for public usage. This is purely for types to make it easier to infer
* available extension types.
*/
public readonly ['~E']!: ExtensionUnion;

/**
* The remirror identifier key.
*
Expand Down
5 changes: 5 additions & 0 deletions @remirror/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export type GetSchema<Type extends { ['~Sch']: unknown }> = Type['~Sch'];
*/
export type GetCommands<Type extends { ['~C']: unknown }> = Type['~C'];

/**
* Get the Extensions from an `EditorManager`, or `Preset`.
*/
export type GetExtensions<Type extends { ['~E']: unknown }> = Type['~E'];

/**
* Get the helpers provided by an from an `EditorManager`, `Extension` or `Preset`.
*/
Expand Down
4 changes: 2 additions & 2 deletions @remirror/extension-code-block/src/code-block-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const CodeBlockExtension = ExtensionFactory.typed<
},
defaultProperties: {
toggleName: 'paragraph',
formatter: () => undefined,
formatter: () => {},
syntaxTheme: 'atomDark',
defaultLanguage: 'markup',
},
Expand Down Expand Up @@ -184,7 +184,7 @@ export const CodeBlockExtension = ExtensionFactory.typed<
];
},

createKeymap({ type, commands, extension }) {
createKeymap({ type, getCommands: commands, extension }) {
return {
Tab: ({ state, dispatch }) => {
const { selection, tr, schema } = state;
Expand Down
10 changes: 9 additions & 1 deletion @remirror/extension-emoji/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@remirror/extension-emoji",
"version": "0.11.0",
"description": "Emoji extension for the remirror wysiwyg editor",
"description": "Add flavor to your editor with emoji's that make the heart sing.",
"keywords": [
"remirror",
"remirror-extension",
"emoji",
"smiley",
"emoticons",
"editor"
],
"homepage": "https://github.com/remirror/remirror/tree/master/@remirror/extension-emoji",
"repository": "https://github.com/remirror/remirror/tree/master/@remirror/extension-emoji",
"license": "MIT",
Expand Down

0 comments on commit 91e668b

Please sign in to comment.