improve types

This commit is contained in:
Philipp Kühn
2021-01-28 09:50:17 +01:00
parent a9c14fbddd
commit 4407d9a3d1
20 changed files with 58 additions and 47 deletions

View File

@@ -11,7 +11,7 @@ import {
* Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
* @param extensions List of extensions
*/
export default function getAttributesFromExtensions(extensions: Extensions) {
export default function getAttributesFromExtensions(extensions: Extensions): ExtensionAttribute[] {
const extensionAttributes: ExtensionAttribute[] = []
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]

View File

@@ -1,8 +1,9 @@
import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model'
import getMarkType from './getMarkType'
import { AnyObject } from '../types'
export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType) {
export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType): AnyObject {
const type = getMarkType(typeOrName, state.schema)
const { from, to, empty } = state.selection
let marks: Mark[] = []

View File

@@ -1,9 +1,5 @@
import { MarkType, ResolvedPos } from 'prosemirror-model'
interface Range {
from: number,
to: number,
}
import { Range } from '../types'
export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void {
if (!$pos || !type) {

View File

@@ -0,0 +1,22 @@
import { Mark } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state'
export type MarkPosition = {
mark: Mark,
start: number,
end: number,
}
export default function getMarksBetween(start: number, end: number, state: EditorState): MarkPosition[] {
let marks: MarkPosition[] = []
state.doc.nodesBetween(start, end, (node, pos) => {
marks = [...marks, ...node.marks.map(mark => ({
start: pos,
end: pos + node.nodeSize,
mark,
}))]
})
return marks
}

View File

@@ -1,8 +1,9 @@
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
import getNodeType from './getNodeType'
import { AnyObject } from '../types'
export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType) {
export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): AnyObject {
const type = getNodeType(typeOrName, state.schema)
const { from, to } = state.selection
let nodes: Node[] = []

View File

@@ -1,6 +1,6 @@
import { Schema } from 'prosemirror-model'
import { MarkType, NodeType, Schema } from 'prosemirror-model'
export default function getSchemaTypeByName(name: string, schema: Schema) {
export default function getSchemaTypeByName(name: string, schema: Schema): NodeType | MarkType | null {
if (schema.nodes[name]) {
return schema.nodes[name]
}

View File

@@ -1,6 +1,6 @@
import { Schema } from 'prosemirror-model'
export default function getSchemaTypeNameByName(name: string, schema: Schema) {
export default function getSchemaTypeNameByName(name: string, schema: Schema): 'node' | 'mark' | null {
if (schema.nodes[name]) {
return 'node'
}

View File

@@ -2,7 +2,7 @@ import { Extensions } from '../types'
import splitExtensions from './splitExtensions'
import callOrReturn from '../utilities/callOrReturn'
export default function isList(name: string, extensions: Extensions) {
export default function isList(name: string, extensions: Extensions): boolean {
const { nodeExtensions } = splitExtensions(extensions)
const extension = nodeExtensions.find(item => item.config.name === name)