Skip to content

Commit

Permalink
Converted draftailmodelchoosers.js to tsx. Added some draft js typing…
Browse files Browse the repository at this point in the history
…s that I know about.
  • Loading branch information
liamjohnston committed Jun 2, 2021
1 parent 8bcb6c6 commit 0851942
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
7 changes: 4 additions & 3 deletions wagtailmodelchoosers/client/DefaultDecorator.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ContentState } from "draft-js";
import React from "react";

type Props = {
entityKey: string;
contentState: any;
children: React.ReactNode;
entityKey?: string;
contentState: ContentState;
children?: React.ReactNode;
};

const DefaultDecorator = (entityData: any, props: Props) => {
Expand Down
31 changes: 0 additions & 31 deletions wagtailmodelchoosers/client/draftailmodelchoosers.js

This file was deleted.

54 changes: 54 additions & 0 deletions wagtailmodelchoosers/client/draftailmodelchoosers.tsx
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;
6 changes: 3 additions & 3 deletions wagtailmodelchoosers/client/sources/ModelSource.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from "react";
import "../polyfills";
import { Modifier, EditorState } from "draft-js";
import { Modifier, EditorState, Entity } from "draft-js";
import ModelPicker from "../components/ModelPicker";

const API_BASE_URL = "/admin/modelchoosers/api/v1";

type Props = {
editorState: any;
editorState: EditorState;
options: any;
entity?: any;
entity?: Entity;
onClose: () => void;
onUpdate: () => void;
onComplete: (nextState: any) => void;
Expand Down
2 changes: 1 addition & 1 deletion wagtailmodelchoosers/client/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
wagtailmodelchoosers:
"./wagtailmodelchoosers/client/wagtailmodelchoosers.tsx",
draftailmodelchoosers:
"./wagtailmodelchoosers/client/draftailmodelchoosers.js",
"./wagtailmodelchoosers/client/draftailmodelchoosers.tsx",
polyfills: "./wagtailmodelchoosers/client/polyfills.js",
},
output: {
Expand Down

0 comments on commit 0851942

Please sign in to comment.