From a656d54bde44cebaa90679c6025329ceb3acecab Mon Sep 17 00:00:00 2001 From: Sergey Dedkov Date: Wed, 8 Feb 2023 22:19:27 +0600 Subject: [PATCH] fix code-highlighting example, add toolbar code block button --- .../examples/code-highlighting.test.ts | 24 ++++++---- site/examples/code-highlighting.tsx | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/playwright/integration/examples/code-highlighting.test.ts b/playwright/integration/examples/code-highlighting.test.ts index f84fef1e56..b70642e1e7 100644 --- a/playwright/integration/examples/code-highlighting.test.ts +++ b/playwright/integration/examples/code-highlighting.test.ts @@ -37,13 +37,17 @@ async function setText(page: Page, text: string, language: string) { } else { await page.keyboard.press('Control+A') } + await page.keyboard.press('Backspace') + + const codeBlockButton = await page.getByTestId('code-block-button') + await codeBlockButton.click({ force: true }) // convert elements to code block await page.keyboard.type(text) - const codeLine = await page - .locator('[data-slate-element-type="code-line"]') + const codeBlock = await page + .locator('[data-slate-element-type="code-block"]') .nth(0) - const select = await codeLine.locator('select').nth(0) + const select = await codeBlock.locator('select').nth(0) await select.selectOption({ value: language }) // Select the language option await expect(await select.inputValue()).toBe(language) // Confirm value to avoid race condition @@ -114,18 +118,18 @@ function getTestCases() { ['"', 'rgb(0, 119, 170)'], [' ', 'rgb(153, 0, 85)'], ['renderIcon', 'rgb(102, 153, 0)'], - ['=', 'rgb(153, 153, 153)'], - ['{', 'rgb(153, 153, 153)'], - ['(', 'rgb(153, 153, 153)'], - [')', 'rgb(153, 153, 153)'], - [' ', 'rgb(0, 0, 0)'], + ['=', 'rgb(153, 0, 85)'], + ['{', 'rgb(153, 0, 85)'], + ['(', 'rgb(153, 0, 85)'], + [')', 'rgb(153, 0, 85)'], + [' ', 'rgb(153, 0, 85)'], ['=>', 'rgb(154, 110, 58)'], - [' ', 'rgb(0, 0, 0)'], + [' ', 'rgb(153, 0, 85)'], ['<', 'rgb(153, 0, 85)'], ['Icon', 'rgb(221, 74, 104)'], [' ', 'rgb(153, 0, 85)'], ['/>', 'rgb(153, 0, 85)'], - ['}', 'rgb(153, 153, 153)'], + ['}', 'rgb(153, 0, 85)'], [' ', 'rgb(153, 0, 85)'], ['/>', 'rgb(153, 0, 85)'], ], diff --git a/site/examples/code-highlighting.tsx b/site/examples/code-highlighting.tsx index 871d8a31b6..81ea3e54fd 100644 --- a/site/examples/code-highlighting.tsx +++ b/site/examples/code-highlighting.tsx @@ -33,6 +33,7 @@ import isHotkey from 'is-hotkey' import { css } from '@emotion/css' import { CodeBlockElement } from './custom-types' import { normalizeTokens } from '../utils/normalize-tokens' +import { Button, Icon, Toolbar } from '../components' const ParagraphType = 'paragraph' const CodeBlockType = 'code-block' @@ -46,6 +47,7 @@ const CodeHighlightingExample = () => { return ( + { const { attributes, children, element } = props const editor = useSlateStatic() + attributes['data-slate-element-type'] = element.type // for elements selecting in integration test + if (element.type === CodeBlockType) { const setLanguage = (language: string) => { const path = ReactEditor.findPath(editor, element) @@ -107,6 +111,46 @@ const renderElement = (props: RenderElementProps) => { ) } +const ExampleToolbar = () => { + return ( + + + + ) +} + +const CodeBlockButton = () => { + const editor = useSlateStatic() + const handleClick = () => { + Transforms.wrapNodes( + editor, + { type: CodeBlockType, language: 'html', children: [] }, + { + match: n => Element.isElement(n) && n.type === ParagraphType, + split: true, + } + ) + Transforms.setNodes( + editor, + { type: CodeLineType }, + { match: n => Element.isElement(n) && n.type === ParagraphType } + ) + } + + return ( + + ) +} + const renderLeaf = (props: RenderLeafProps) => { const { attributes, children, leaf } = props const { text, ...rest } = leaf @@ -317,6 +361,10 @@ declare module 'slate' { } }`), }, + { + type: ParagraphType, + children: toChildren('There you have it!'), + }, ] // Prismjs theme stored as a string instead of emotion css function.