mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-08 08:16:30 +02:00
TypeScript Improvement: Use [key: string]: unknown
, not [key: string]: any
(#3566)
* Change all [key:string]:any -> unknown Skipping slate-hyperscript, since it seems to be an appropriate use of key: any * Minor typo fix docs/api/locations * Check/assert unknown attributes when needed
This commit is contained in:
@@ -28,7 +28,7 @@ type Path = number[]
|
|||||||
interface Point {
|
interface Point {
|
||||||
path: Path
|
path: Path
|
||||||
offset: number
|
offset: number
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ Options: `{affinity?: 'forward' | 'backward' | null}`
|
|||||||
interface Range {
|
interface Range {
|
||||||
anchor: Point
|
anchor: Point
|
||||||
focus: Point
|
focus: Point
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -123,7 +123,7 @@ interface Editor {
|
|||||||
selection: Range | null
|
selection: Range | null
|
||||||
operations: Operation[]
|
operations: Operation[]
|
||||||
marks: Record<string, any> | null
|
marks: Record<string, any> | null
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
|
|
||||||
// Schema-specific node behaviors.
|
// Schema-specific node behaviors.
|
||||||
isInline: (element: Element) => boolean
|
isInline: (element: Element) => boolean
|
||||||
@@ -212,7 +212,7 @@ Apply an operation in the editor.
|
|||||||
```typescript
|
```typescript
|
||||||
interface Element {
|
interface Element {
|
||||||
children: Node[]
|
children: Node[]
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ Check if an element matches a set of `props`. Note: This checks custom propertie
|
|||||||
```typescript
|
```typescript
|
||||||
interface Text {
|
interface Text {
|
||||||
text: string,
|
text: string,
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ Slate works with pure JSON objects. All it requires is that those JSON objects c
|
|||||||
```ts
|
```ts
|
||||||
interface Text {
|
interface Text {
|
||||||
text: string
|
text: string
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ To take another example, the `Element` node interface in Slate is:
|
|||||||
```ts
|
```ts
|
||||||
interface Element {
|
interface Element {
|
||||||
children: Node[]
|
children: Node[]
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -55,7 +55,7 @@ Elements make up the middle layers of a richtext document. They are the nodes th
|
|||||||
```ts
|
```ts
|
||||||
interface Element {
|
interface Element {
|
||||||
children: Node[]
|
children: Node[]
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ Text nodes are the lowest-level nodes in the tree, containing the text content o
|
|||||||
```ts
|
```ts
|
||||||
interface Text {
|
interface Text {
|
||||||
text: string
|
text: string
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -37,7 +37,7 @@ Points are slightly more specific than paths, and contain an `offset` into a spe
|
|||||||
interface Point {
|
interface Point {
|
||||||
path: Path
|
path: Path
|
||||||
offset: number
|
offset: number
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ Ranges are a way to refer not just to a single point in the document, but to a w
|
|||||||
interface Range {
|
interface Range {
|
||||||
anchor: Point
|
anchor: Point
|
||||||
focus: Point
|
focus: Point
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ interface Editor {
|
|||||||
selection: Range | null
|
selection: Range | null
|
||||||
operations: Operation[]
|
operations: Operation[]
|
||||||
marks: Record<string, any> | null
|
marks: Record<string, any> | null
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
|
|
||||||
// Schema-specific node behaviors.
|
// Schema-specific node behaviors.
|
||||||
isInline: (element: Element) => boolean
|
isInline: (element: Element) => boolean
|
||||||
|
@@ -708,7 +708,7 @@ export const Editable = (props: EditableProps) => {
|
|||||||
if (Hotkeys.isRedo(nativeEvent)) {
|
if (Hotkeys.isRedo(nativeEvent)) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
if (editor.redo) {
|
if (typeof editor.redo === 'function') {
|
||||||
editor.redo()
|
editor.redo()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,7 +718,7 @@ export const Editable = (props: EditableProps) => {
|
|||||||
if (Hotkeys.isUndo(nativeEvent)) {
|
if (Hotkeys.isUndo(nativeEvent)) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
if (editor.undo) {
|
if (typeof editor.undo === 'function') {
|
||||||
editor.undo()
|
editor.undo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -43,7 +43,7 @@ const Leaf = (props: {
|
|||||||
opacity: '0.333',
|
opacity: '0.333',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{leaf.placeholder}
|
{leaf.placeholder as React.ReactNode}
|
||||||
</span>
|
</span>
|
||||||
{children}
|
{children}
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
|
@@ -17,7 +17,7 @@ export const Slate = (props: {
|
|||||||
value: Node[]
|
value: Node[]
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
onChange: (value: Node[]) => void
|
onChange: (value: Node[]) => void
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}) => {
|
}) => {
|
||||||
const { editor, children, onChange, value, ...rest } = props
|
const { editor, children, onChange, value, ...rest } = props
|
||||||
const [key, setKey] = useState(0)
|
const [key, setKey] = useState(0)
|
||||||
|
@@ -38,7 +38,7 @@ export interface Editor {
|
|||||||
selection: Range | null
|
selection: Range | null
|
||||||
operations: Operation[]
|
operations: Operation[]
|
||||||
marks: Record<string, any> | null
|
marks: Record<string, any> | null
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
|
|
||||||
// Schema-specific node behaviors.
|
// Schema-specific node behaviors.
|
||||||
isInline: (element: Element) => boolean
|
isInline: (element: Element) => boolean
|
||||||
@@ -1332,7 +1332,7 @@ export const Editor = {
|
|||||||
// the operation was applied.
|
// the operation was applied.
|
||||||
parent.children.splice(index, 1)
|
parent.children.splice(index, 1)
|
||||||
const truePath = Path.transform(path, op)!
|
const truePath = Path.transform(path, op)!
|
||||||
const newParent = Node.get(editor, Path.parent(truePath))
|
const newParent = Node.get(editor, Path.parent(truePath)) as Ancestor
|
||||||
const newIndex = truePath[truePath.length - 1]
|
const newIndex = truePath[truePath.length - 1]
|
||||||
|
|
||||||
newParent.children.splice(newIndex, 0, node)
|
newParent.children.splice(newIndex, 0, node)
|
||||||
|
@@ -9,7 +9,7 @@ import { Editor, Node, Path } from '..'
|
|||||||
|
|
||||||
export interface Element {
|
export interface Element {
|
||||||
children: Node[]
|
children: Node[]
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Element = {
|
export const Element = {
|
||||||
|
@@ -5,7 +5,7 @@ export type InsertNodeOperation = {
|
|||||||
type: 'insert_node'
|
type: 'insert_node'
|
||||||
path: Path
|
path: Path
|
||||||
node: Node
|
node: Node
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InsertTextOperation = {
|
export type InsertTextOperation = {
|
||||||
@@ -13,7 +13,7 @@ export type InsertTextOperation = {
|
|||||||
path: Path
|
path: Path
|
||||||
offset: number
|
offset: number
|
||||||
text: string
|
text: string
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MergeNodeOperation = {
|
export type MergeNodeOperation = {
|
||||||
@@ -22,21 +22,21 @@ export type MergeNodeOperation = {
|
|||||||
position: number
|
position: number
|
||||||
target: number | null
|
target: number | null
|
||||||
properties: Partial<Node>
|
properties: Partial<Node>
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MoveNodeOperation = {
|
export type MoveNodeOperation = {
|
||||||
type: 'move_node'
|
type: 'move_node'
|
||||||
path: Path
|
path: Path
|
||||||
newPath: Path
|
newPath: Path
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RemoveNodeOperation = {
|
export type RemoveNodeOperation = {
|
||||||
type: 'remove_node'
|
type: 'remove_node'
|
||||||
path: Path
|
path: Path
|
||||||
node: Node
|
node: Node
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RemoveTextOperation = {
|
export type RemoveTextOperation = {
|
||||||
@@ -44,7 +44,7 @@ export type RemoveTextOperation = {
|
|||||||
path: Path
|
path: Path
|
||||||
offset: number
|
offset: number
|
||||||
text: string
|
text: string
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SetNodeOperation = {
|
export type SetNodeOperation = {
|
||||||
@@ -52,25 +52,25 @@ export type SetNodeOperation = {
|
|||||||
path: Path
|
path: Path
|
||||||
properties: Partial<Node>
|
properties: Partial<Node>
|
||||||
newProperties: Partial<Node>
|
newProperties: Partial<Node>
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SetSelectionOperation =
|
export type SetSelectionOperation =
|
||||||
| {
|
| {
|
||||||
type: 'set_selection'
|
type: 'set_selection'
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
properties: null
|
properties: null
|
||||||
newProperties: Range
|
newProperties: Range
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
type: 'set_selection'
|
type: 'set_selection'
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
properties: Partial<Range>
|
properties: Partial<Range>
|
||||||
newProperties: Partial<Range>
|
newProperties: Partial<Range>
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
type: 'set_selection'
|
type: 'set_selection'
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
properties: Range
|
properties: Range
|
||||||
newProperties: null
|
newProperties: null
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ export type SplitNodeOperation = {
|
|||||||
position: number
|
position: number
|
||||||
target: number | null
|
target: number | null
|
||||||
properties: Partial<Node>
|
properties: Partial<Node>
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NodeOperation =
|
export type NodeOperation =
|
||||||
|
@@ -12,7 +12,7 @@ import { Operation, Path } from '..'
|
|||||||
export interface Point {
|
export interface Point {
|
||||||
path: Path
|
path: Path
|
||||||
offset: number
|
offset: number
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Point = {
|
export const Point = {
|
||||||
|
@@ -11,7 +11,7 @@ import { Operation, Path, Point, PointEntry } from '..'
|
|||||||
export interface Range {
|
export interface Range {
|
||||||
anchor: Point
|
anchor: Point
|
||||||
focus: Point
|
focus: Point
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Range = {
|
export const Range = {
|
||||||
|
@@ -9,7 +9,7 @@ import { Range } from '..'
|
|||||||
|
|
||||||
export interface Text {
|
export interface Text {
|
||||||
text: string
|
text: string
|
||||||
[key: string]: any
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Text = {
|
export const Text = {
|
||||||
|
@@ -10,7 +10,7 @@ import {
|
|||||||
Descendant,
|
Descendant,
|
||||||
NodeEntry,
|
NodeEntry,
|
||||||
Path,
|
Path,
|
||||||
Transforms,
|
Ancestor,
|
||||||
} from '..'
|
} from '..'
|
||||||
|
|
||||||
export const GeneralTransforms = {
|
export const GeneralTransforms = {
|
||||||
@@ -104,7 +104,7 @@ export const GeneralTransforms = {
|
|||||||
// the operation was applied.
|
// the operation was applied.
|
||||||
parent.children.splice(index, 1)
|
parent.children.splice(index, 1)
|
||||||
const truePath = Path.transform(path, op)!
|
const truePath = Path.transform(path, op)!
|
||||||
const newParent = Node.get(editor, Path.parent(truePath))
|
const newParent = Node.get(editor, Path.parent(truePath)) as Ancestor
|
||||||
const newIndex = truePath[truePath.length - 1]
|
const newIndex = truePath[truePath.length - 1]
|
||||||
|
|
||||||
newParent.children.splice(newIndex, 0, node)
|
newParent.children.splice(newIndex, 0, node)
|
||||||
|
@@ -8,6 +8,8 @@ import {
|
|||||||
Range,
|
Range,
|
||||||
Text,
|
Text,
|
||||||
Transforms,
|
Transforms,
|
||||||
|
NodeEntry,
|
||||||
|
Ancestor,
|
||||||
} from '..'
|
} from '..'
|
||||||
|
|
||||||
export const NodeTransforms = {
|
export const NodeTransforms = {
|
||||||
@@ -168,7 +170,8 @@ export const NodeTransforms = {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const [parent, parentPath] = Editor.node(editor, Path.parent(path))
|
const parentNodeEntry = Editor.node(editor, Path.parent(path))
|
||||||
|
const [parent, parentPath] = parentNodeEntry as NodeEntry<Ancestor>
|
||||||
const index = path[path.length - 1]
|
const index = path[path.length - 1]
|
||||||
const { length } = parent.children
|
const { length } = parent.children
|
||||||
|
|
||||||
@@ -721,7 +724,7 @@ export const NodeTransforms = {
|
|||||||
|
|
||||||
for (const pathRef of pathRefs) {
|
for (const pathRef of pathRefs) {
|
||||||
const path = pathRef.unref()!
|
const path = pathRef.unref()!
|
||||||
const [node] = Editor.node(editor, path)
|
const [node] = Editor.node(editor, path) as NodeEntry<Ancestor>
|
||||||
let range = Editor.range(editor, path)
|
let range = Editor.range(editor, path)
|
||||||
|
|
||||||
if (split && rangeRef) {
|
if (split && rangeRef) {
|
||||||
@@ -823,7 +826,8 @@ export const NodeTransforms = {
|
|||||||
: Path.common(firstPath, lastPath)
|
: Path.common(firstPath, lastPath)
|
||||||
|
|
||||||
const range = Editor.range(editor, firstPath, lastPath)
|
const range = Editor.range(editor, firstPath, lastPath)
|
||||||
const [commonNode] = Editor.node(editor, commonPath)
|
const commonNodeEntry = Editor.node(editor, commonPath)
|
||||||
|
const [commonNode] = commonNodeEntry as NodeEntry<Ancestor>
|
||||||
const depth = commonPath.length + 1
|
const depth = commonPath.length + 1
|
||||||
const wrapperPath = Path.next(lastPath.slice(0, depth))
|
const wrapperPath = Path.next(lastPath.slice(0, depth))
|
||||||
const wrapper = { ...element, children: [] }
|
const wrapper = { ...element, children: [] }
|
||||||
|
Reference in New Issue
Block a user