Skip to content

Commit fba00f3

Browse files
john-traasadrianschmidt
authored andcommitted
chore(text editor): add typing to function params
1 parent 01ffc15 commit fba00f3

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
removeListNodes,
1717
convertAllListNodes,
1818
toggleList,
19+
Dispatch,
1920
} from './utils/list-utils';
2021
import { adjustSelectionToFullBlocks } from './utils/selection-utils';
2122
import { copyPasteLinkCommand } from './utils/link-utils';
@@ -225,7 +226,13 @@ const createWrapInCommand = (
225226
* @param dispatch - The dispatch function.
226227
* @returns A command for handling list operations when there is no selection.
227228
*/
228-
const handleListNoSelection = (state, type, schema, otherType, dispatch) => {
229+
const handleListNoSelection = (
230+
state: EditorState,
231+
type: NodeType,
232+
schema: Schema,
233+
otherType: NodeType,
234+
dispatch: Dispatch,
235+
) => {
229236
const { $from } = state.selection;
230237
const blockFrom = $from.start();
231238
const blockTo = $from.end();
@@ -258,7 +265,13 @@ const handleListNoSelection = (state, type, schema, otherType, dispatch) => {
258265
* @param dispatch - The dispatch function.
259266
* @returns A command for handling list operations when there is a selection.
260267
*/
261-
const handleListWithSelection = (state, type, schema, otherType, dispatch) => {
268+
const handleListWithSelection = (
269+
state: EditorState,
270+
type: NodeType,
271+
schema: Schema,
272+
otherType: NodeType,
273+
dispatch: Dispatch,
274+
) => {
262275
const { $from } = state.selection;
263276
const listItemType = schema.nodes.list_item;
264277
const ancestorDepth = findAncestorDepthOfType($from, listItemType);
@@ -298,7 +311,10 @@ const handleListWithSelection = (state, type, schema, otherType, dispatch) => {
298311
* @param listTypeName - The name of the list type to toggle.
299312
* @returns A command for toggling list types.
300313
*/
301-
export const createListCommand = (schema, listTypeName) => {
314+
export const createListCommand = (
315+
schema: Schema,
316+
listTypeName: string,
317+
): CommandWithActive => {
302318
const type = schema.nodes[listTypeName];
303319
if (!type) {
304320
throw new Error(`List type "${listTypeName}" not found in schema`);

src/components/text-editor/prosemirror-adapter/menu/utils/list-utils.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { EditorState } from 'prosemirror-state';
2+
import { EditorState, Transaction } from 'prosemirror-state';
33
import { NodeType, Fragment, Schema } from 'prosemirror-model';
44
import { EditorMenuTypes } from '../types';
55
import { findWrapping, liftTarget } from 'prosemirror-transform';
@@ -40,7 +40,14 @@ export const getOtherListType = (
4040
return schema.nodes[otherType];
4141
};
4242

43-
export const removeListNodes = (state, targetType, schema, dispatch) => {
43+
export type Dispatch = (tr: Transaction) => void;
44+
45+
export const removeListNodes = (
46+
state: EditorState,
47+
targetType: NodeType,
48+
schema: Schema,
49+
dispatch: Dispatch,
50+
) => {
4451
let tr = state.tr;
4552
let changed = false;
4653

@@ -100,7 +107,11 @@ const fromBulletToOrderedList = (fromType: NodeType, toType: NodeType) => {
100107
);
101108
};
102109

103-
const convertListAttributes = (fromType, toType, attrs) => {
110+
const convertListAttributes = (
111+
fromType: NodeType,
112+
toType: NodeType,
113+
attrs: Record<string, any>,
114+
) => {
104115
const newAttrs = { ...attrs };
105116
if (fromOrderedToBulletList(fromType, toType)) {
106117
// Bullet lists generally do not need an "order" attribute.
@@ -113,7 +124,12 @@ const convertListAttributes = (fromType, toType, attrs) => {
113124
return newAttrs;
114125
};
115126

116-
export const convertAllListNodes = (state, fromType, toType, dispatch) => {
127+
export const convertAllListNodes = (
128+
state: EditorState,
129+
fromType: NodeType,
130+
toType: NodeType,
131+
dispatch: Dispatch,
132+
) => {
117133
let converted = false;
118134
let tr = state.tr;
119135

@@ -149,8 +165,8 @@ export const convertAllListNodes = (state, fromType, toType, dispatch) => {
149165
return converted;
150166
};
151167

152-
export const toggleList = (listType) => {
153-
return (state, dispatch) => {
168+
export const toggleList = (listType: NodeType) => {
169+
return (state: EditorState, dispatch: Dispatch) => {
154170
const { $from, $to } = state.selection;
155171
const range = $from.blockRange($to);
156172

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { TextSelection } from 'prosemirror-state';
1+
import { TextSelection, EditorState } from 'prosemirror-state';
22

3-
export const adjustSelectionToFullBlocks = (state) => {
3+
export const adjustSelectionToFullBlocks = (state: EditorState) => {
44
const { $from, $to } = state.selection;
55
const from = $from.pos === $from.start() ? $from.pos : $from.end();
66
const to = $to.pos === $to.end() ? $to.pos : $to.start();
77

88
return { from: from, to: to };
99
};
1010

11-
export const createBlockSelection = (state, from, to) => {
11+
export const createBlockSelection = (
12+
state: EditorState,
13+
from: number,
14+
to: number,
15+
) => {
1216
return new TextSelection(state.doc.resolve(from), state.doc.resolve(to));
1317
};

0 commit comments

Comments
 (0)