Skip to content

Commit

Permalink
feat: @remirror/core works after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed May 20, 2020
1 parent ef3ada3 commit 5dc8e70
Show file tree
Hide file tree
Showing 24 changed files with 448 additions and 318 deletions.
6 changes: 6 additions & 0 deletions @remirror/core-constants/src/error-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export enum ErrorConstant {
*/
MANAGER_PHASE_ERROR = 'RMR0008',

/**
* No directly invoking the editor manager with `new`. Instead use one of the
* static methods to create your instance.
*/
NEW_EDITOR_MANAGER = 'RMR0009',

/**
* The user requested an invalid extension from the preset. Please check the
* `createExtensions` return method is returning an extension with the defined
Expand Down
6 changes: 4 additions & 2 deletions @remirror/core-helpers/src/core-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ if (process.env.NODE !== 'production') {
[ErrorConstant.MISSING_REQUIRED_EXTENSION]: 'Your editor is missing a required extension.',
[ErrorConstant.MANAGER_PHASE_ERROR]:
'Called a method event at the wrong time. Please make sure getter functions are only called with within the scope of the returned functions. They should not be called in the outer scope of your method.',
[ErrorConstant.NEW_EDITOR_MANAGER]:
'No directly invoking the editor manager with `new`. Instead use one of the available static methods to create your instance.',
[ErrorConstant.INVALID_PRESET_EXTENSION]:
'You requested an invalid extension from the preset. Please check the `createExtensions` return method is returning an extension with the requested constructor.',
[ErrorConstant.INVALID_MANAGER_ARGUMENTS]:
Expand Down Expand Up @@ -62,8 +64,8 @@ function isErrorConstant(code: unknown): code is ErrorConstant {
*/
function createErrorMessage(code: ErrorConstant, extraMessage?: string) {
const message = errorMessageMap[code];
const prefix = message ? `${message}\n` : '';
const customMessage = extraMessage ? `${extraMessage}\n` : '';
const prefix = message ? `${message}\n\n` : '';
const customMessage = extraMessage ? `${extraMessage}\n\n` : '';

return `${prefix}${customMessage}For more information visit ${ERROR_INFORMATION_URL}#${code}`;
}
Expand Down
5 changes: 5 additions & 0 deletions @remirror/core-types/src/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export interface Shape {
*/
export type AnyFunction<Type = any> = (...args: any[]) => Type;

/**
* Matches any constructor type.
*/
export type AnyConstructor = new (...args: any) => any;

/**
* Make the whole interface partial except for some specified keys which will be
* made required.
Expand Down
11 changes: 8 additions & 3 deletions @remirror/core/src/builtins/tags-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ export class TagsExtension extends PlainExtension {
mark[group] = isUndefined(mark[group]) ? [name] : [...mark[group], name];
}

for (const tag of extension.tags as ExtensionTag[]) {
general[tag] = isUndefined(general[tag])
if (!extension.tags) {
return;
}

for (const tag of extension.tags) {
const generalTag = general[tag];
general[tag] = isUndefined(generalTag)
? [extension.name]
: [...general[tag], extension.name];
: [...generalTag, extension.name];
}
},
afterExtensionLoop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ describe('Manager', () => {
const bigExtension = new BigExtension({ priority: ExtensionPriority.Lowest });
const corePreset = new CorePreset({});

let manager = new EditorManager({
let manager = EditorManager.of({
extensions: [dummyExtension, bigExtension],
presets: [corePreset],
});

let view: EditorView;

beforeEach(() => {
manager = new EditorManager({
manager = EditorManager.of({
extensions: [dummyExtension, bigExtension],
presets: [new CorePreset({})],
});
Expand Down Expand Up @@ -201,13 +201,14 @@ test('keymaps', () => {
}

public createKeymap = () => {
console.log('creating the keymap 3');
return {
Enter: mocks.thirdEnter,
};
};
}

const manager = new EditorManager({
const manager = EditorManager.of({
extensions: [new FirstExtension(), new SecondExtension(), new ThirdExtension()],
presets: [new CorePreset({})],
});
Expand Down
61 changes: 31 additions & 30 deletions @remirror/core/src/editor-manager/editor-manager-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { invariant, isEmptyArray, isFunction, object, sort } from '@remirror/cor

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

export interface TransformExtensionOrPreset<
ExtensionUnion extends AnyExtension,
Expand All @@ -18,11 +18,11 @@ export interface TransformExtensionOrPreset<
/**
* Transforms the unsorted array of presets and extension into presets and
* sorted extensions. Handles uniqueness of extensions and automatically pulling
* in required extensions.
* throwing an error when required extensions are missing.
*
* TODO Add a check for requiredExtensions and inject them automatically
* TODO Currently matching by constructor - what if different versions exist in
* the same app...
* TODO Add a check for requiredExtensions and inject them automatically TODO
* Currently matching by constructor - what if different versions exist in the
* same app...
*
* @param unionValues - the extensions to transform as well as their priorities
*
Expand All @@ -34,34 +34,29 @@ export function transformExtensionOrPreset<
>(
unionValues: ReadonlyArray<ExtensionUnion | PresetUnion>,
): TransformExtensionOrPreset<ExtensionUnion, PresetUnion> {
type BuiltinExtensionUnion = ExtensionUnion;
type BuiltinPresetUnion = PresetUnion;
type ExtensionConstructor = GetConstructor<BuiltinExtensionUnion>;
type PresetConstructor = GetConstructor<BuiltinPresetUnion>;
type ExtensionConstructor = GetConstructor<ExtensionUnion>;
type PresetConstructor = GetConstructor<PresetUnion>;
interface MissingConstructor {
Constructor: AnyExtensionConstructor;
extension: BuiltinExtensionUnion;
extension: ExtensionUnion;
}

// The items to return.
const presets: BuiltinPresetUnion[] = [];
const presetMap = new WeakMap<PresetConstructor, BuiltinPresetUnion>();
const extensions: BuiltinExtensionUnion[] = [];
const extensionMap = new WeakMap<ExtensionConstructor, BuiltinExtensionUnion>();
const presets: PresetUnion[] = [];
const presetMap = new WeakMap<PresetConstructor, PresetUnion>();
const extensions: ExtensionUnion[] = [];
const extensionMap = new WeakMap<ExtensionConstructor, ExtensionUnion>();

// Used to track duplicates and the presets they've been added by.
const duplicateMap = new WeakMap<
AnyExtensionConstructor,
Array<BuiltinPresetUnion | undefined>
>();
const duplicateMap = new WeakMap<AnyExtensionConstructor, Array<PresetUnion | undefined>>();

// The unsorted, de-duped, unrefined extensions.
let rawExtensions: BuiltinExtensionUnion[] = [];
let rawExtensions: ExtensionUnion[] = [];

/**
* Adds the values to the duplicate map for checking duplicates.
*/
const updateDuplicateMap = (extension: BuiltinExtensionUnion, preset?: BuiltinPresetUnion) => {
const updateDuplicateMap = (extension: ExtensionUnion, preset?: PresetUnion) => {
const key = extension.constructor;
const duplicate = duplicateMap.get(key);
duplicateMap.set(key as never, duplicate ? [...duplicate, preset] : [preset]);
Expand All @@ -79,11 +74,11 @@ export function transformExtensionOrPreset<
// Update the presets list in this block
if (isPreset(presetOrExtension)) {
presets.push(presetOrExtension);
rawExtensions.push(...presetOrExtension.extensions);
rawExtensions.push(...(presetOrExtension.extensions as ExtensionUnion[]));
presetMap.set(presetOrExtension.constructor, presetOrExtension);

presetOrExtension.extensions.forEach((extension) =>
updateDuplicateMap(extension, presetOrExtension),
updateDuplicateMap(extension as ExtensionUnion, presetOrExtension),
);

continue;
Expand Down Expand Up @@ -120,23 +115,26 @@ export function transformExtensionOrPreset<
extensions.push(extension);

// Replace the extensions for all presets that referenced this constructor.
duplicates.forEach((preset) => preset?.replaceExtension(key, extension as Of<typeof key>));
duplicates.forEach((preset) => preset?.replaceExtension(key, extension));
}

const missing: MissingConstructor[] = [];

/**
* Populate the missing Constructors.
*/
const findMissingExtensions = (extension: BuiltinExtensionUnion) => {
for (const Constructor of extension.requiredExtensions) {
if (found.has(Constructor)) {
const findMissingExtensions = (extension: ExtensionUnion) => {
if (!extension.requiredExtensions) {
return;
}

for (const Constructor of extension.requiredExtensions ?? []) {
if (found.has(Constructor as AnyExtensionConstructor)) {
continue;
}

missing.push({ Constructor, extension });
missing.push({ Constructor: Constructor as AnyExtensionConstructor, extension });
}
extension.requiredExtensions.every((Constructor) => found.has(Constructor));
};

// Throw if any required extension missing.
Expand All @@ -149,7 +147,7 @@ export function transformExtensionOrPreset<
message: missing
.map(
({ Constructor, extension }) =>
`The extension '${extension.name}' requires '${Constructor.extensionName} in order to run correctly.`,
`The extension '${extension.name}' requires '${Constructor.name} in order to run correctly.`,
)
.join('\n'),
});
Expand Down Expand Up @@ -195,7 +193,10 @@ export enum ManagerPhase {
None,

/**
* When the extension manager is being created.
* When the extension manager is being created and the onCreate methods are
* being called.
*
* This happens within the EditorManager constructor.
*/
Create,

Expand Down

0 comments on commit 5dc8e70

Please sign in to comment.