Skip to content

Commit

Permalink
encode data-uuid passed to CKEditor (#1063)
Browse files Browse the repository at this point in the history
In #1036 we began encoding the data-uuid argument for < resource_link > shortcodes. The encoding / decoding worked fine for converting between HTML and Markdown.

But in that PR I forgot to encode the data we passed to CKEditor when creating new links.
  • Loading branch information
ChristopherChudzicki authored Feb 25, 2022
1 parent 1b4cb49 commit 6d6e087
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
32 changes: 31 additions & 1 deletion static/js/components/widgets/MarkdownEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from "react"
import { shallow } from "enzyme"
import ClassicEditor from "@ckeditor/ckeditor5-editor-classic/src/classiceditor"
import { CKEditor } from "@ckeditor/ckeditor5-react"
import sinon, { SinonSandbox } from "sinon"
import { omit } from "ramda"

import MarkdownEditor from "./MarkdownEditor"
import {
FullEditorConfig,
MinimalEditorConfig
MinimalEditorConfig,
insertResourceLink
} from "../../lib/ckeditor/CKEditor"
import {
ADD_RESOURCE_EMBED,
Expand All @@ -16,6 +18,18 @@ import {
RESOURCE_EMBED,
RESOURCE_LINK
} from "../../lib/ckeditor/plugins/constants"
import ResourcePickerDialog from "../../components/widgets/ResourcePickerDialog"
import { getMockEditor } from "../../test_util"

jest.mock("../../lib/ckeditor/CKEditor", () => {
const originalModule = jest.requireActual("../../lib/ckeditor/CKEditor")

return {
__esModule: true,
...originalModule,
insertResourceLink: jest.fn()
}
})

jest.mock("@ckeditor/ckeditor5-react", () => ({
CKEditor: () => <div />
Expand Down Expand Up @@ -63,6 +77,22 @@ describe("MarkdownEditor", () => {
})
})

it("should delegate to addResourceLink when inserting a link", async () => {
const wrapper = render({ link: ["page"] })
const editorComponent = wrapper.find<{ onReady:(e: unknown) => void }>(
CKEditor
)
const editor = getMockEditor()
editorComponent.prop("onReady")(editor)
const picker = wrapper.find(ResourcePickerDialog)
picker.prop("insertEmbed")("best-uuid-ever", "some title", "resourceLink")
expect(insertResourceLink).toHaveBeenCalledWith(
editor,
"best-uuid-ever",
"some title"
)
})

it.each([
{ link: [], embed: [], shouldExist: false },
{ link: ["page"], embed: [], shouldExist: true },
Expand Down
5 changes: 3 additions & 2 deletions static/js/components/widgets/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import ClassicEditor from "@ckeditor/ckeditor5-editor-classic/src/classiceditor"

import {
FullEditorConfig,
MinimalEditorConfig
MinimalEditorConfig,
insertResourceLink
} from "../../lib/ckeditor/CKEditor"
import EmbeddedResource from "./EmbeddedResource"
import {
Expand Down Expand Up @@ -57,7 +58,7 @@ export default function MarkdownEditor(props: Props): JSX.Element {
// we pass the title down because we want to set that as the
// default text in the link, in the case where we're not adding
// the link attribute to existing text.
editor.current.execute(ResourceCommandMap[variant], uuid, title)
insertResourceLink(editor.current, uuid, title)
} else {
editor.current.execute(ResourceCommandMap[variant], uuid)
}
Expand Down
6 changes: 5 additions & 1 deletion static/js/components/widgets/ResourcePickerDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ describe("ResourcePickerDialog", () => {
})

it.each([
{ mode: RESOURCE_LINK, attaching: "linking", acceptText: "Add link" },
{
mode: RESOURCE_LINK,
attaching: "linking",
acceptText: "Add link"
},
{
mode: RESOURCE_EMBED,
attaching: "embedding",
Expand Down
16 changes: 15 additions & 1 deletion static/js/lib/ckeditor/CKEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ import ParagraphPlugin from "@ckeditor/ckeditor5-paragraph/src/paragraph"
import TablePlugin from "@ckeditor/ckeditor5-table/src/table"
import TableToolbarPlugin from "@ckeditor/ckeditor5-table/src/tabletoolbar"

import { editor } from "@ckeditor/ckeditor5-core"

import Markdown from "./plugins/Markdown"
import ResourceEmbed from "./plugins/ResourceEmbed"
import ResourcePicker from "./plugins/ResourcePicker"
import { ADD_RESOURCE_EMBED, ADD_RESOURCE_LINK } from "./plugins/constants"
import ResourceLink from "@mitodl/ckeditor5-resource-link/src/link"
import ResourceLinkMarkdownSyntax from "./plugins/ResourceLinkMarkdownSyntax"
import { RESOURCE_LINK_COMMAND } from "@mitodl/ckeditor5-resource-link/src/constants"
import ResourceLinkMarkdownSyntax, {
encodeShortcodeArgs
} from "./plugins/ResourceLinkMarkdownSyntax"
import DisallowNestedTables from "./plugins/DisallowNestedTables"
import TableMarkdownSyntax from "./plugins/TableMarkdownSyntax"
import MarkdownListSyntax from "./plugins/MarkdownListSyntax"
Expand Down Expand Up @@ -111,3 +116,12 @@ export const MinimalEditorConfig = {
},
language: "en"
}

export const insertResourceLink = (
editor: editor.Editor,
uuid: string,
title: string
) => {
const encoded = encodeShortcodeArgs(uuid)
editor.execute(RESOURCE_LINK_COMMAND, encoded, title)
}
8 changes: 8 additions & 0 deletions static/js/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ export const twoBooleanTestMatrix = [
[false, true],
[false, false]
]

export const getMockEditor = () => ({
editing: {
view: {
focus: jest.fn()
}
}
})

0 comments on commit 6d6e087

Please sign in to comment.