1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-14 03:03:58 +02:00

Custom TypeScript Types (#3835)

This PR adds better TypeScript types into Slate and is based on the proposal here: https://github.com/ianstormtaylor/slate/issues/3725

* Extend Slate's types like Element and Text

* Supports type discrimination (ie. if an element has type === "table" then we get a reduced set of properties)

* added custom types

* files

* more extensions

* files

* changed fixtures

* changes eslint file

* changed element.children to descendant

* updated types

* more type changes

* changed a lot of typing, still getting building errors

* extended text type in slate-react

* removed type assertions

* Clean up of custom types and a couple uneeded comments.

* Rename headingElement-true.tsx.tsx to headingElement-true.tsx

* moved basetext and baselement

* Update packages/slate/src/interfaces/text.ts

Co-authored-by: Brent Farese <25846953+BrentFarese@users.noreply.github.com>

* Fix some type issues with core functions.

* Clean up text and element files.

* Convert other types to extended types.

* Change the type of editor.marks to the appropriate type.

* Add version 100.0.0 to package.json

* Revert "Add version 100.0.0 to package.json"

This reverts commit 329e44e43d.

* added custom types

* files

* more extensions

* files

* changed fixtures

* changes eslint file

* changed element.children to descendant

* updated types

* more type changes

* changed a lot of typing, still getting building errors

* extended text type in slate-react

* removed type assertions

* Clean up of custom types and a couple uneeded comments.

* Rename headingElement-true.tsx.tsx to headingElement-true.tsx

* moved basetext and baselement

* Update packages/slate/src/interfaces/text.ts

Co-authored-by: Brent Farese <25846953+BrentFarese@users.noreply.github.com>

* Fix some type issues with core functions.

* Clean up text and element files.

* Convert other types to extended types.

* Change the type of editor.marks to the appropriate type.

* Run linter.

* Remove key:string uknown from the base types.

* Clean up types after removing key:string unknown.

* Lint and prettier fixes.

* Implement custom-types

Co-authored-by: mdmjg <mdj308@nyu.edu>

* added custom types to examples

* reset yarn lock

* added ts to fixtures

* examples custom types

* Working fix

* ts-thesunny-try

* Extract interface types.

* Fix minor return type in create-editor.

* Fix the typing issue with Location having compile time CustomTypes

* Extract types for Transforms.

* Update README.

* Fix dependency on slate-history in slate-react

Co-authored-by: mdmjg <mdj308@nyu.edu>
Co-authored-by: Brent Farese <brentfarese@gmail.com>
Co-authored-by: Brent Farese <25846953+BrentFarese@users.noreply.github.com>
Co-authored-by: Tim Buckley <timothypbuckley@gmail.com>
This commit is contained in:
Sunny Hirai
2020-11-24 12:30:06 -08:00
committed by GitHub
parent a5f4170162
commit 08275f68f3
61 changed files with 3111 additions and 3387 deletions

View File

@@ -24,6 +24,7 @@
},
"devDependencies": {
"slate": "^0.59.0",
"slate-history": "^0.59.0",
"slate-hyperscript": "^0.59.0"
},
"peerDependencies": {

View File

@@ -9,6 +9,7 @@ import {
Transforms,
Path,
} from 'slate'
import { HistoryEditor } from 'slate-history'
import throttle from 'lodash/throttle'
import scrollIntoView from 'scroll-into-view-if-needed'
@@ -766,7 +767,7 @@ export const Editable = (props: EditableProps) => {
if (Hotkeys.isRedo(nativeEvent)) {
event.preventDefault()
if (typeof editor.redo === 'function') {
if (HistoryEditor.isHistoryEditor(editor)) {
editor.redo()
}
@@ -776,7 +777,7 @@ export const Editable = (props: EditableProps) => {
if (Hotkeys.isUndo(nativeEvent)) {
event.preventDefault()
if (typeof editor.undo === 'function') {
if (HistoryEditor.isHistoryEditor(editor)) {
editor.undo()
}

View File

@@ -1,6 +1,5 @@
import React from 'react'
import { Text, Element } from 'slate'
import { Element, Text } from 'slate'
import String from './string'
import { PLACEHOLDER_SYMBOL } from '../utils/weak-maps'
import { RenderLeafProps } from './editable'
@@ -46,7 +45,7 @@ const Leaf = (props: {
textDecoration: 'none',
}}
>
{leaf.placeholder as React.ReactNode}
{leaf.placeholder}
</span>
{children}
</React.Fragment>
@@ -75,10 +74,6 @@ const MemoizedLeaf = React.memo(Leaf, (prev, next) => {
)
})
/**
* The default custom leaf renderer.
*/
export const DefaultLeaf = (props: RenderLeafProps) => {
const { attributes, children } = props
return <span {...attributes}>{children}</span>

View File

@@ -1,5 +1,5 @@
import React, { useMemo, useState, useCallback, useEffect } from 'react'
import { Node } from 'slate'
import { Node, Element, Descendant } from 'slate'
import { ReactEditor } from '../plugin/react-editor'
import { FocusedContext } from '../hooks/use-focused'
@@ -14,10 +14,9 @@ import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'
export const Slate = (props: {
editor: ReactEditor
value: Node[]
value: Descendant[]
children: React.ReactNode
onChange: (value: Node[]) => void
[key: string]: unknown
onChange: (value: Descendant[]) => void
}) => {
const { editor, children, onChange, value, ...rest } = props
const [key, setKey] = useState(0)

View File

@@ -0,0 +1,12 @@
import { CustomTypes } from 'slate'
declare module 'slate' {
interface CustomTypes {
Text: {
placeholder: string
}
Range: {
placeholder?: string
}
}
}

View File

@@ -12,6 +12,7 @@ import DOMText = globalThis.Text
import DOMRange = globalThis.Range
import DOMSelection = globalThis.Selection
import DOMStaticRange = globalThis.StaticRange
export {
DOMNode,
DOMComment,