-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new `extensionDecorator` function which augments the static properties of an `Extension` constructor when used as a decorator.
- Loading branch information
Showing
9 changed files
with
407 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
'@remirror/core': minor | ||
'@remirror/core-types': patch | ||
--- | ||
|
||
Add new `extensionDecorator` function which augments the static properties of an `Extension` constructor when used as a decorator. | ||
|
||
The following code will add a decorator to the extension. | ||
|
||
```ts | ||
import { PlainExtension, ExtensionPriority, extensionDecorator } from 'remirror/core'; | ||
|
||
interface ExampleOptions { | ||
color?: string; | ||
|
||
/** | ||
* This option is annotated as a handler and needs a static property. | ||
**/ | ||
onChange?: Handler<() => void>; | ||
} | ||
|
||
@extensionDecorator<ExampleOptions>({ | ||
defaultOptions: { color: 'red' }, | ||
defaultPriority: ExtensionPriority.Lowest, | ||
handlerKeys: ['onChange'], | ||
}) | ||
class ExampleExtension extends PlainExtension<ExampleOptions> { | ||
get name() { | ||
return 'example' as const; | ||
} | ||
} | ||
``` | ||
|
||
The extension decorator updates the static properties of the extension. If you prefer not to use decorators it can also be called as a function. The `Extension` constructor is mutated by the function call and does not need to be returned. | ||
|
||
```ts | ||
extensionDecorator({ defaultSettings: { color: 'red' } })(ExampleExtension); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { CustomHandler, Dynamic, Handler, Static } from '@remirror/core-types'; | ||
|
||
import { extensionDecorator } from '..'; | ||
import { PlainExtension } from '../extension'; | ||
|
||
interface GeneralOptions { | ||
type: Static<'awesome' | 'not-awesome'>; | ||
color?: string; | ||
backgroundColor?: Dynamic<string>; | ||
onChange?: Handler<() => void>; | ||
keyBindings: CustomHandler<Record<string, () => boolean>>; | ||
} | ||
|
||
describe('@extensionDecorator', () => { | ||
it('can decorate an extension', () => { | ||
@extensionDecorator<GeneralOptions>({ | ||
defaultOptions: { backgroundColor: 'red', color: 'pink' }, | ||
staticKeys: ['type'], | ||
handlerKeys: ['onChange'], | ||
customHandlerKeys: ['keyBindings'], | ||
}) | ||
class TestExtension extends PlainExtension<GeneralOptions> { | ||
get name() { | ||
return 'test' as const; | ||
} | ||
} | ||
|
||
expect(TestExtension.staticKeys).toEqual(['type']); | ||
expect(TestExtension.handlerKeys).toEqual(['onChange']); | ||
expect(TestExtension.customHandlerKeys).toEqual(['keyBindings']); | ||
expect(TestExtension.defaultOptions).toEqual({ backgroundColor: 'red', color: 'pink' }); | ||
}); | ||
|
||
it('can decorate an extension as a function call', () => { | ||
const TestExtension = extensionDecorator<GeneralOptions>({ | ||
defaultOptions: { backgroundColor: 'red', color: 'pink' }, | ||
staticKeys: ['type'], | ||
handlerKeys: ['onChange'], | ||
customHandlerKeys: ['keyBindings'], | ||
})( | ||
class TestExtension extends PlainExtension<GeneralOptions> { | ||
get name() { | ||
return 'test' as const; | ||
} | ||
}, | ||
); | ||
|
||
expect(TestExtension.staticKeys).toEqual(['type']); | ||
expect(TestExtension.handlerKeys).toEqual(['onChange']); | ||
expect(TestExtension.customHandlerKeys).toEqual(['keyBindings']); | ||
expect(TestExtension.defaultOptions).toEqual({ backgroundColor: 'red', color: 'pink' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.