refactoring

This commit is contained in:
Philipp Kühn 2020-12-01 09:20:25 +01:00
parent bc39e922ca
commit fe9f74ebdf
5 changed files with 13 additions and 6 deletions

View File

@ -2,8 +2,9 @@ import { EditorState } from 'prosemirror-state'
import isNodeActive from './isNodeActive'
import isMarkActive from './isMarkActive'
import getSchemaTypeNameByName from './getSchemaTypeNameByName'
import { AnyObject } from '../types'
export default function isActive(state: EditorState, name: string | null, attributes: { [key: string ]: any } = {}): boolean {
export default function isActive(state: EditorState, name: string | null, attributes: AnyObject = {}): boolean {
if (!name) {
return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes)
}

View File

@ -2,6 +2,7 @@ import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes'
import getMarkType from './getMarkType'
import { AnyObject } from '../types'
export type MarkRange = {
mark: Mark,
@ -12,7 +13,7 @@ export type MarkRange = {
export default function isMarkActive(
state: EditorState,
typeOrName: MarkType | string | null,
attributes = {},
attributes: AnyObject = {},
): boolean {
const { from, to, empty } = state.selection
const type = typeOrName

View File

@ -2,6 +2,7 @@ import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes'
import getNodeType from './getNodeType'
import { AnyObject } from '../types'
export type NodeRange = {
node: Node,
@ -12,7 +13,7 @@ export type NodeRange = {
export default function isNodeActive(
state: EditorState,
typeOrName: NodeType | string | null,
attributes = {},
attributes: AnyObject = {},
): boolean {
const { from, to, empty } = state.selection
const type = typeOrName

View File

@ -1,16 +1,18 @@
import { AnyObject } from '../types'
/**
* Remove a property or an array of properties from an object
* @param obj Object
* @param key Key to remove
*/
export default function deleteProps(obj: { [key: string ]: any }, propOrProps: string | string[]) {
export default function deleteProps(obj: AnyObject, propOrProps: string | string[]) {
const props = typeof propOrProps === 'string'
? [propOrProps]
: propOrProps
return Object
.keys(obj)
.reduce((newObj: { [key: string ]: any }, prop) => {
.reduce((newObj: AnyObject, prop) => {
if (!props.includes(prop)) {
newObj[prop] = obj[prop]
}

View File

@ -1,9 +1,11 @@
import { AnyObject } from '../types'
/**
* Check if object1 includes object2
* @param object1 Object
* @param object2 Object
*/
export default function objectIncludes(object1: { [key: string ]: any }, object2: { [key: string ]: any }): boolean {
export default function objectIncludes(object1: AnyObject, object2: AnyObject): boolean {
const keys = Object.keys(object2)
if (!keys.length) {