-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted draftailmodelchoosers.js to tsx. Added some draft js typing…
…s that I know about.
- Loading branch information
1 parent
8bcb6c6
commit 0851942
Showing
5 changed files
with
62 additions
and
38 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
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
import ModelSource from "./sources/ModelSource"; | ||
import DefaultDecorator from "./DefaultDecorator"; | ||
|
||
const WrapDecorator = | ||
(entityData, decorator) => | ||
(...args) => | ||
decorator(entityData, ...args); | ||
|
||
type Props = { | ||
modelChooserEntityTypes: any[]; | ||
draftailOptions: any; | ||
widgetAttrIds: any; | ||
}; | ||
|
||
const modelChooserDraftailInit = ({ | ||
modelChooserEntityTypes, | ||
draftailOptions, | ||
widgetAttrIds, | ||
}: Props) => { | ||
// Save entities for decorators to use. Doing this because I can't find a way to get entity type | ||
// data from within a decorator without saving it to contentstate, which ends up getting saved | ||
// to DB, which is unnecessary and breaks historical data. | ||
|
||
// Typescript doesn't know about .fromEntries, but it has been polyfilled for IE. | ||
// @ts-ignore | ||
const modelChooserDraftailEntities = Object.fromEntries( | ||
draftailOptions.entityTypes | ||
.filter((et) => modelChooserEntityTypes.includes(et.type)) | ||
.map((et) => [et.type, et]) | ||
); | ||
|
||
// Register all modelchooser entity plugins. | ||
modelChooserEntityTypes.forEach((entityType) => { | ||
const decorators = (window as any).draftailDecorators || {}; | ||
const decorator = decorators[entityType] || DefaultDecorator; | ||
const plugin = { | ||
type: entityType, | ||
source: ModelSource, | ||
decorator: WrapDecorator( | ||
modelChooserDraftailEntities[entityType], | ||
decorator | ||
), | ||
}; | ||
(window as any).draftail.registerPlugin(plugin); | ||
}); | ||
|
||
(window as any).draftail.initEditor( | ||
widgetAttrIds, | ||
draftailOptions, | ||
document.currentScript | ||
); | ||
}; | ||
|
||
(window as any).modelChooserDraftailInit = modelChooserDraftailInit; |
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