Skip to content

Commit e0fc9ec

Browse files
authored
Add new Minimal with Math Editor for Enabling MathJax (#2869)
* Add new Minimal with Math editor * Add test for new editor config * Simplify logic to remove branching
1 parent d58c2af commit e0fc9ec

File tree

6 files changed

+84
-40
lines changed

6 files changed

+84
-40
lines changed

static/js/components/widgets/MarkdownEditor.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import MarkdownEditor from "./MarkdownEditor"
88
import {
99
FullEditorConfig,
1010
MinimalEditorConfig,
11+
MinimalWithMathEditorConfig,
1112
} from "../../lib/ckeditor/CKEditor"
1213
import {
1314
ADD_RESOURCE_EMBED,
1415
ADD_RESOURCE_LINK,
1516
CKEDITOR_RESOURCE_UTILS,
1617
MARKDOWN_CONFIG_KEY,
18+
MINIMAL_WITH_MATH,
1719
RESOURCE_EMBED,
1820
RESOURCE_LINK,
1921
RESOURCE_LINK_CONFIG_KEY,
@@ -103,6 +105,11 @@ describe("MarkdownEditor", () => {
103105
configLabel: "FullEditorConfig",
104106
otherProps: { allowedHtml: ["sub", "sup"] },
105107
},
108+
{
109+
minimal: MINIMAL_WITH_MATH,
110+
expectedConfig: MinimalWithMathEditorConfig,
111+
configLabel: "MinimalWithMathEditorConfig",
112+
},
106113
])(
107114
"Uses the $configLabel when minimal=$minimal",
108115
({ minimal, expectedConfig, otherProps }) => {

static/js/components/widgets/MarkdownEditor.tsx

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CKEditorInspector from "@ckeditor/ckeditor5-inspector"
77
import {
88
FullEditorConfig,
99
MinimalEditorConfig,
10+
MinimalWithMathEditorConfig,
1011
} from "../../lib/ckeditor/CKEditor"
1112
import ResourceLink from "../../lib/ckeditor/plugins/ResourceLink"
1213
import { checkNotSubAndSup } from "../../lib/ckeditor/attributeChecks"
@@ -23,6 +24,7 @@ import {
2324
MARKDOWN_CONFIG_KEY,
2425
RESOURCE_LINK_CONFIG_KEY,
2526
WEBSITE_NAME,
27+
MINIMAL_WITH_MATH,
2628
} from "../../lib/ckeditor/plugins/constants"
2729
import ResourcePickerDialog from "./ResourcePickerDialog"
2830
import useThrowSynchronously from "../../hooks/useAsyncError"
@@ -37,7 +39,7 @@ export interface Props {
3739
name?: string
3840
onChange?: (event: { target: { value: string; name: string } }) => void
3941
children?: React.ReactNode
40-
minimal?: boolean
42+
minimal?: boolean | typeof MINIMAL_WITH_MATH
4143
embed: string[]
4244
link: string[]
4345
allowedHtml: string[]
@@ -142,48 +144,34 @@ export default function MarkdownEditor(props: Props): JSX.Element {
142144
[WEBSITE_NAME]: website.name,
143145
}
144146

147+
const baseConfig =
148+
minimal === MINIMAL_WITH_MATH
149+
? MinimalWithMathEditorConfig
150+
: minimal
151+
? MinimalEditorConfig
152+
: FullEditorConfig
153+
145154
// Create a copy of plugins to avoid mutating the original
146-
const plugins = minimal
147-
? [...MinimalEditorConfig.plugins]
148-
: [...FullEditorConfig.plugins]
155+
const plugins = [...baseConfig.plugins]
149156
if (isCustomLinkUIEnabled) {
150157
plugins.push(CustomLink)
151158
}
152159

153-
if (minimal) {
154-
return {
155-
...MinimalEditorConfig,
156-
plugins,
157-
[CKEDITOR_RESOURCE_UTILS]: {
158-
renderResource,
159-
openResourcePicker,
160-
},
161-
toolbar: {
162-
...MinimalEditorConfig.toolbar,
163-
items: MinimalEditorConfig.toolbar.items.filter(toolbarItemsFilter),
164-
},
165-
...resourceLink,
166-
}
167-
} else {
168-
// this render function is stuck into the editor config
169-
// our ResourceEmbed plugin can pull the callback out,
170-
// and then use it to render resources within the editor.
171-
return {
172-
...FullEditorConfig,
173-
plugins,
174-
[CKEDITOR_RESOURCE_UTILS]: {
175-
renderResource,
176-
openResourcePicker,
177-
},
178-
toolbar: {
179-
...FullEditorConfig.toolbar,
180-
items: FullEditorConfig.toolbar.items.filter(toolbarItemsFilter),
181-
},
182-
[MARKDOWN_CONFIG_KEY]: {
183-
allowedHtml,
184-
},
185-
...resourceLink,
186-
}
160+
return {
161+
...baseConfig,
162+
plugins,
163+
[CKEDITOR_RESOURCE_UTILS]: {
164+
renderResource,
165+
openResourcePicker,
166+
},
167+
toolbar: {
168+
...baseConfig.toolbar,
169+
items: baseConfig.toolbar.items.filter(toolbarItemsFilter),
170+
},
171+
[MARKDOWN_CONFIG_KEY]: {
172+
allowedHtml,
173+
},
174+
...resourceLink,
187175
}
188176
}, [
189177
minimal,

static/js/lib/ckeditor/CKEditor.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,48 @@ export const MinimalEditorConfig = {
172172
},
173173
language: "en",
174174
}
175+
176+
export const MinimalWithMathEditorConfig = {
177+
plugins: [
178+
EssentialsPlugin,
179+
AutoformatPlugin,
180+
BoldPlugin,
181+
ItalicPlugin,
182+
CodePlugin,
183+
BlockQuotePlugin,
184+
LinkPlugin,
185+
ListPlugin,
186+
ParagraphPlugin,
187+
ResourceEmbed,
188+
ResourcePicker,
189+
ResourceLink,
190+
MathSyntax,
191+
MarkdownListSyntax,
192+
Mathematics,
193+
Markdown,
194+
LegacyShortcodes,
195+
],
196+
toolbar: {
197+
items: [
198+
"bold",
199+
"italic",
200+
"code",
201+
"link",
202+
"bulletedList",
203+
"numberedList",
204+
"blockQuote",
205+
"math",
206+
"undo",
207+
"redo",
208+
ADD_RESOURCE_LINK,
209+
ADD_RESOURCE_EMBED,
210+
],
211+
},
212+
math: {
213+
engine: "mathjax",
214+
outputType: "script",
215+
forceOutputType: true,
216+
enablePreview: true,
217+
},
218+
language: "en",
219+
}

static/js/lib/ckeditor/plugins/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export const ADD_RESOURCE_LINK = "addResourceLink"
22

33
export const ADD_RESOURCE_EMBED = "addResourceEmbed"
44

5+
export const MINIMAL_WITH_MATH = "with-math" as const
6+
57
export const CKEDITOR_RESOURCE_UTILS = "CKEDITOR_RESOURCE_UTILS"
68

79
export const RESOURCE_EMBED = "resourceEmbed"

static/js/types/websites.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ interface ConfigFieldBaseProps {
4949
condition?: FieldValueCondition
5050
}
5151

52+
import { MINIMAL_WITH_MATH } from "../lib/ckeditor/plugins/constants"
53+
5254
export interface MarkdownConfigField extends ConfigFieldBaseProps {
5355
widget: WidgetVariant.Markdown
54-
minimal?: boolean
56+
minimal?: boolean | typeof MINIMAL_WITH_MATH
5557
link?: string[]
5658
embed?: string[]
5759
allowed_html?: string[]

websites/config_schema/site-config-schema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ field:
2525
'object', 'relation', 'menu', 'hierarchical-select', 'website-collection')
2626
label: str()
2727
name: str()
28-
minimal: bool(required=False)
28+
minimal: any(bool(), enum('with-math'), required=False)
2929
allowed_html: list(str(), required=False)
3030
required: bool(required=False)
3131
help: str(required=False)

0 commit comments

Comments
 (0)