-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from prezly/feature/html-blocks
[MT-4552] Feature - HTML blocks support
- Loading branch information
Showing
14 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/slate-editor/src/modules/editor-v4-html/HtmlExtension.tsx
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,32 @@ | ||
import type { Extension } from '@prezly/slate-commons'; | ||
import { createDeserializeElement } from '@prezly/slate-commons'; | ||
import { HTML_NODE_TYPE, isHtmlNode } from '@prezly/slate-types'; | ||
import React from 'react'; | ||
import type { RenderElementProps } from 'slate-react'; | ||
|
||
import { HtmlElement } from './components'; | ||
import { HTML_EXTENSION_ID } from './constants'; | ||
import { normalizeRedundantHtmlBlockAttributes, parseSerializedElement } from './lib'; | ||
|
||
export const HtmlExtension = (): Extension => ({ | ||
deserialize: { | ||
element: { | ||
[HTML_NODE_TYPE]: createDeserializeElement(parseSerializedElement), | ||
}, | ||
}, | ||
id: HTML_EXTENSION_ID, | ||
normalizers: [normalizeRedundantHtmlBlockAttributes], | ||
renderElement: ({ attributes, children, element }: RenderElementProps) => { | ||
if (isHtmlNode(element)) { | ||
return ( | ||
<HtmlElement attributes={attributes} element={element}> | ||
{children} | ||
</HtmlElement> | ||
); | ||
} | ||
|
||
return undefined; | ||
}, | ||
rootTypes: [HTML_NODE_TYPE], | ||
voidTypes: [HTML_NODE_TYPE], | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/slate-editor/src/modules/editor-v4-html/components/HtmlElement.tsx
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,25 @@ | ||
import type { HtmlNode } from '@prezly/slate-types'; | ||
import type { PropsWithChildren } from 'react'; | ||
import React from 'react'; | ||
import type { RenderElementProps } from 'slate-react'; | ||
|
||
import { EditorBlock } from '#components'; | ||
|
||
interface Props extends RenderElementProps { | ||
element: HtmlNode; | ||
} | ||
|
||
export function HtmlElement({ attributes, children, element }: PropsWithChildren<Props>) { | ||
return ( | ||
<EditorBlock | ||
{...attributes} // contains `ref` | ||
element={element} | ||
extendedHitArea | ||
renderBlock={() => <pre>{element.content}</pre>} | ||
void | ||
> | ||
{/* We have to render children or Slate will fail when trying to find the node. */} | ||
{children} | ||
</EditorBlock> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/slate-editor/src/modules/editor-v4-html/components/index.ts
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 @@ | ||
export { HtmlElement } from './HtmlElement'; |
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 @@ | ||
export const HTML_EXTENSION_ID = 'HtmlExtension'; |
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,3 @@ | ||
export { HTML_EXTENSION_ID } from './constants'; | ||
export { HtmlExtension } from './HtmlExtension'; | ||
export { createHtmlBlock } from './lib'; |
10 changes: 10 additions & 0 deletions
10
packages/slate-editor/src/modules/editor-v4-html/lib/createHtmlBlock.ts
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,10 @@ | ||
import type { HtmlNode } from '@prezly/slate-types'; | ||
import { HTML_NODE_TYPE } from '@prezly/slate-types'; | ||
|
||
export function createHtmlBlock({ content }: Pick<HtmlNode, 'content'>): HtmlNode { | ||
return { | ||
type: HTML_NODE_TYPE, | ||
content, | ||
children: [{ text: '' }], | ||
}; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/slate-editor/src/modules/editor-v4-html/lib/index.ts
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,3 @@ | ||
export { createHtmlBlock } from './createHtmlBlock'; | ||
export { normalizeRedundantHtmlBlockAttributes } from './normalizeRedundantHtmlBlockAttributes'; | ||
export { parseSerializedElement } from './parseSerializedElement'; |
18 changes: 18 additions & 0 deletions
18
...ages/slate-editor/src/modules/editor-v4-html/lib/normalizeRedundantHtmlBlockAttributes.ts
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,18 @@ | ||
import { EditorCommands } from '@prezly/slate-commons'; | ||
import { isHtmlNode } from '@prezly/slate-types'; | ||
import type { Editor, NodeEntry } from 'slate'; | ||
|
||
import { createHtmlBlock } from './createHtmlBlock'; | ||
|
||
const ALLOWED_ATTRIBUTES = Object.keys(createHtmlBlock({ content: '' })); | ||
|
||
export function normalizeRedundantHtmlBlockAttributes( | ||
editor: Editor, | ||
[node, path]: NodeEntry, | ||
): boolean { | ||
if (!isHtmlNode(node)) { | ||
return false; | ||
} | ||
|
||
return EditorCommands.normalizeRedundantAttributes(editor, [node, path], ALLOWED_ATTRIBUTES); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/slate-editor/src/modules/editor-v4-html/lib/parseSerializedElement.ts
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,14 @@ | ||
import type { HtmlNode } from '@prezly/slate-types'; | ||
import { isHtmlNode } from '@prezly/slate-types'; | ||
|
||
import { createHtmlBlock } from './createHtmlBlock'; | ||
|
||
export function parseSerializedElement(serialized: string): HtmlNode | undefined { | ||
const parsed = JSON.parse(serialized); | ||
|
||
if (isHtmlNode(parsed)) { | ||
return createHtmlBlock(parsed); | ||
} | ||
|
||
return undefined; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/slate-editor/src/modules/editor-v4-html/transforms/index.ts
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 @@ | ||
export { removeHtmlBlock } from './removeHtmlBlock'; |
9 changes: 9 additions & 0 deletions
9
packages/slate-editor/src/modules/editor-v4-html/transforms/removeHtmlBlock.ts
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,9 @@ | ||
import { EditorCommands } from '@prezly/slate-commons'; | ||
import { isHtmlNode } from '@prezly/slate-types'; | ||
import type { Editor } from 'slate'; | ||
|
||
export function removeHtmlBlock(editor: Editor): void { | ||
EditorCommands.removeNode(editor, { | ||
match: isHtmlNode, | ||
}); | ||
} |
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,13 @@ | ||
import type { ElementNode } from './ElementNode'; | ||
import { isElementNode } from './ElementNode'; | ||
|
||
export const HTML_NODE_TYPE = 'html'; | ||
|
||
export interface HtmlNode extends ElementNode { | ||
type: typeof HTML_NODE_TYPE; | ||
content: string; | ||
} | ||
|
||
export function isHtmlNode(value: any): value is HtmlNode { | ||
return isElementNode<HtmlNode>(value, HTML_NODE_TYPE); | ||
} |
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