From 4f992cff5cfa4af8aec96356b1178df74e102c19 Mon Sep 17 00:00:00 2001 From: Ravi Lamkoti <44892121+RavenColEvol@users.noreply.github.com> Date: Mon, 10 Mar 2025 21:50:10 +0530 Subject: [PATCH] Fix example types (#5812) * fix: types for richtext.tsx * fix: types for check-lists, code-highlighting and custom placeholder * fix: types for editable-voids, embeds, forced-layout, hovering-toolbar * fix: types for remaining examples * fix: typescript error for files in image element * fix: types for examples and some minor fixes * fix: normalize-tokens.ts type * fix: types for [example].tsx --- .../04-applying-custom-formatting.md | 8 +- package.json | 2 + site/examples/js/check-lists.jsx | 2 +- site/examples/js/code-highlighting.jsx | 34 ++--- site/examples/js/components/index.jsx | 47 +------ site/examples/js/custom-placeholder.jsx | 2 +- site/examples/js/editable-voids.jsx | 10 +- site/examples/js/forced-layout.jsx | 8 +- site/examples/js/hovering-toolbar.jsx | 14 +- site/examples/js/huge-document.jsx | 18 +-- site/examples/js/iframe.jsx | 8 +- site/examples/js/images.jsx | 39 +++--- site/examples/js/inlines.jsx | 18 +-- site/examples/js/markdown-preview.jsx | 4 +- site/examples/js/mentions.jsx | 24 ++-- site/examples/js/paste-html.jsx | 25 ++-- site/examples/js/richtext.jsx | 45 +++++-- site/examples/js/search-highlighting.jsx | 16 ++- site/examples/js/tables.jsx | 6 +- site/examples/ts/check-lists.tsx | 55 +++++--- site/examples/ts/code-highlighting.tsx | 83 +++++++----- site/examples/ts/components/index.tsx | 72 +--------- site/examples/ts/custom-placeholder.tsx | 9 +- site/examples/ts/custom-types.d.ts | 65 ++++++++- site/examples/ts/editable-voids.tsx | 34 +++-- site/examples/ts/embeds.tsx | 31 +++-- site/examples/ts/forced-layout.tsx | 33 +++-- site/examples/ts/hovering-toolbar.tsx | 46 ++++--- site/examples/ts/huge-document.tsx | 33 +++-- site/examples/ts/iframe.tsx | 60 ++++++--- site/examples/ts/images.tsx | 72 +++++----- site/examples/ts/inlines.tsx | 92 ++++++++----- site/examples/ts/markdown-preview.tsx | 30 +++-- site/examples/ts/markdown-shortcuts.tsx | 30 +++-- site/examples/ts/mentions.tsx | 77 +++++++---- site/examples/ts/paste-html.tsx | 99 +++++++++----- site/examples/ts/richtext.tsx | 123 +++++++++++++----- site/examples/ts/search-highlighting.tsx | 45 +++++-- site/examples/ts/tables.tsx | 37 ++++-- site/examples/ts/utils/normalize-tokens.ts | 4 +- site/pages/examples/[example].tsx | 49 ++++--- site/tsconfig.json | 2 +- yarn.lock | 16 +++ 43 files changed, 935 insertions(+), 592 deletions(-) diff --git a/docs/walkthroughs/04-applying-custom-formatting.md b/docs/walkthroughs/04-applying-custom-formatting.md index 5fea3c7c2..df89f965a 100644 --- a/docs/walkthroughs/04-applying-custom-formatting.md +++ b/docs/walkthroughs/04-applying-custom-formatting.md @@ -90,7 +90,9 @@ const App = () => { Transforms.setNodes( editor, { type: match ? 'paragraph' : 'code' }, - { match: n => Element.isElement(n) && Editor.isBlock(editor, n) } + { + match: n => Element.isElement(n) && Editor.isBlock(editor, n), + } ) break } @@ -178,7 +180,9 @@ const App = () => { Transforms.setNodes( editor, { type: match ? null : 'code' }, - { match: n => Element.isElement(n) && Editor.isBlock(editor, n) } + { + match: n => Element.isElement(n) && Editor.isBlock(editor, n), + } ) break } diff --git a/package.json b/package.json index 65ade27ec..291463871 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,8 @@ "@emotion/css": "^11.11.2", "@faker-js/faker": "^8.2.0", "@playwright/test": "^1.39.0", + "@types/is-hotkey": "^0.1.10", + "@types/is-url": "^1.2.32", "@types/jest": "29.5.6", "@types/lodash": "^4.14.200", "@types/mocha": "^10.0.3", diff --git a/site/examples/js/check-lists.jsx b/site/examples/js/check-lists.jsx index 6e56ed77d..36084fc80 100644 --- a/site/examples/js/check-lists.jsx +++ b/site/examples/js/check-lists.jsx @@ -121,9 +121,9 @@ const Element = props => { } } const CheckListItemElement = ({ attributes, children, element }) => { + const { checked } = element const editor = useSlateStatic() const readOnly = useReadOnly() - const { checked } = element return (
{ return useCallback( ([node, path]) => { if (Element.isElement(node) && node.type === CodeLineType) { - const ranges = editor.nodeToDecorations.get(node) || [] + const ranges = editor.nodeToDecorations?.get(node) || [] return ranges } return [] diff --git a/site/examples/js/components/index.jsx b/site/examples/js/components/index.jsx index 072558217..bb6c725ec 100644 --- a/site/examples/js/components/index.jsx +++ b/site/examples/js/components/index.jsx @@ -1,6 +1,6 @@ +import { css, cx } from '@emotion/css' import React from 'react' import ReactDOM from 'react-dom' -import { cx, css } from '@emotion/css' export const Button = React.forwardRef( ({ className, active, reversed, ...props }, ref) => ( @@ -23,51 +23,6 @@ export const Button = React.forwardRef( /> ) ) -export const EditorValue = React.forwardRef( - ({ className, value, ...props }, ref) => { - const textLines = value.document.nodes - .map(node => node.text) - .toArray() - .join('\n') - return ( -
-
- Slate's value as text -
-
- {textLines} -
-
- ) - } -) export const Icon = React.forwardRef(({ className, ...props }, ref) => ( { const editor = useMemo( diff --git a/site/examples/js/forced-layout.jsx b/site/examples/js/forced-layout.jsx index b6b815ffe..5186ba1c1 100644 --- a/site/examples/js/forced-layout.jsx +++ b/site/examples/js/forced-layout.jsx @@ -1,13 +1,13 @@ import React, { useCallback, useMemo } from 'react' -import { Slate, Editable, withReact } from 'slate-react' import { - Transforms, - createEditor, + Editor, Node, Element as SlateElement, - Editor, + Transforms, + createEditor, } from 'slate' import { withHistory } from 'slate-history' +import { Editable, Slate, withReact } from 'slate-react' const withLayout = editor => { const { normalizeNode } = editor diff --git a/site/examples/js/hovering-toolbar.jsx b/site/examples/js/hovering-toolbar.jsx index 002d7f4e6..9282aadc7 100644 --- a/site/examples/js/hovering-toolbar.jsx +++ b/site/examples/js/hovering-toolbar.jsx @@ -1,8 +1,8 @@ -import React, { useMemo, useRef, useEffect } from 'react' -import { Slate, Editable, withReact, useSlate, useFocused } from 'slate-react' -import { Editor, createEditor, Range } from 'slate' import { css } from '@emotion/css' +import React, { useEffect, useMemo, useRef } from 'react' +import { Editor, Range, createEditor } from 'slate' import { withHistory } from 'slate-history' +import { Editable, Slate, useFocused, useSlate, withReact } from 'slate-react' import { Button, Icon, Menu, Portal } from './components' const HoveringMenuExample = () => { @@ -23,7 +23,7 @@ const HoveringMenuExample = () => { return toggleMark(editor, 'italic') case 'formatUnderline': event.preventDefault() - return toggleMark(editor, 'underlined') + return toggleMark(editor, 'underline') } }} /> @@ -49,13 +49,13 @@ const Leaf = ({ attributes, children, leaf }) => { if (leaf.italic) { children = {children} } - if (leaf.underlined) { + if (leaf.underline) { children = {children} } return {children} } const HoveringToolbar = () => { - const ref = useRef() + const ref = useRef(null) const editor = useSlate() const inFocus = useFocused() useEffect(() => { @@ -105,7 +105,7 @@ const HoveringToolbar = () => { > - + ) diff --git a/site/examples/js/huge-document.jsx b/site/examples/js/huge-document.jsx index 789c01948..deae2380e 100644 --- a/site/examples/js/huge-document.jsx +++ b/site/examples/js/huge-document.jsx @@ -1,21 +1,23 @@ -import React, { useMemo, useCallback } from 'react' import { faker } from '@faker-js/faker' +import React, { useCallback, useMemo } from 'react' import { createEditor } from 'slate' -import { Slate, Editable, withReact } from 'slate-react' +import { Editable, Slate, withReact } from 'slate-react' const HEADINGS = 100 const PARAGRAPHS = 7 const initialValue = [] for (let h = 0; h < HEADINGS; h++) { - initialValue.push({ - type: 'heading', + const heading = { + type: 'heading-one', children: [{ text: faker.lorem.sentence() }], - }) + } + initialValue.push(heading) for (let p = 0; p < PARAGRAPHS; p++) { - initialValue.push({ + const paragraph = { type: 'paragraph', children: [{ text: faker.lorem.paragraph() }], - }) + } + initialValue.push(paragraph) } } const HugeDocumentExample = () => { @@ -29,7 +31,7 @@ const HugeDocumentExample = () => { } const Element = ({ attributes, children, element }) => { switch (element.type) { - case 'heading': + case 'heading-one': return

{children}

default: return

{children}

diff --git a/site/examples/js/iframe.jsx b/site/examples/js/iframe.jsx index 51fac9fd6..45d7877ad 100644 --- a/site/examples/js/iframe.jsx +++ b/site/examples/js/iframe.jsx @@ -1,9 +1,9 @@ +import isHotkey from 'is-hotkey' import React, { useCallback, useMemo, useState } from 'react' import { createPortal } from 'react-dom' -import isHotkey from 'is-hotkey' -import { Editable, withReact, useSlate, Slate, ReactEditor } from 'slate-react' import { Editor, createEditor } from 'slate' import { withHistory } from 'slate-history' +import { Editable, ReactEditor, Slate, useSlate, withReact } from 'slate-react' import { Button, Icon, Toolbar } from './components' const HOTKEYS = { @@ -93,7 +93,9 @@ const MarkButton = ({ format, icon }) => { const IFrame = ({ children, ...props }) => { const [iframeBody, setIframeBody] = useState(null) const handleLoad = e => { - setIframeBody(e.target.contentDocument.body) + const iframe = e.target + if (!iframe.contentDocument) return + setIframeBody(iframe.contentDocument.body) } return (