-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
config.ts
56 lines (48 loc) · 1.69 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright 2021, Milkdown by Mirone. */
import { createSlice, Ctx } from '@milkdown/core';
import { Node, NodeType } from '@milkdown/prose/model';
import { Transaction } from '@milkdown/prose/state';
import { swap } from './utils';
export type ShouldSyncNode = (context: {
prevNode: Node;
nextNode: Node;
ctx: Ctx;
tr: Transaction;
text: string;
}) => boolean;
export type SyncNodePlaceholder = {
hole: string;
punctuation: string;
char: string;
};
export type InlineSyncConfig = {
placeholderConfig: SyncNodePlaceholder;
shouldSyncNode: ShouldSyncNode;
globalNodes: Array<NodeType | string>;
movePlaceholder: (placeholderToMove: string, text: string) => string;
};
export const defaultConfig: InlineSyncConfig = {
placeholderConfig: {
hole: '∅',
punctuation: '⁂',
char: '∴',
},
globalNodes: ['footnote_definition'],
shouldSyncNode: ({ prevNode, nextNode }) =>
prevNode.inlineContent &&
nextNode &&
// if node type changes, do not sync
prevNode.type === nextNode.type &&
// if two node fully equal, we don't modify them
!prevNode.eq(nextNode),
movePlaceholder: (placeholderToMove: string, text: string) => {
const symbolsNeedToMove = ['*', '_'];
let index = text.indexOf(placeholderToMove);
while (symbolsNeedToMove.includes(text[index - 1] || '') && symbolsNeedToMove.includes(text[index + 1] || '')) {
text = swap(text, index, index + 1);
index = index + 1;
}
return text;
},
};
export const inlineSyncConfigCtx = createSlice<InlineSyncConfig, 'inlineSyncConfig'>(defaultConfig, 'inlineSyncConfig');