-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathblockUtils.ts
128 lines (119 loc) · 3.74 KB
/
blockUtils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type {MarkdownTextInputElement} from '../../MarkdownTextInput.web';
import type {InlineImagesInputProps, MarkdownRange} from '../../commonTypes';
import type {PartialMarkdownStyle} from '../../styleUtils';
import {addInlineImagePreview} from '../inputElements/inlineImage';
import type {NodeType, TreeNode} from './treeUtils';
function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownStyle: PartialMarkdownStyle, isMultiline = true) {
const node = targetElement;
switch (type) {
case 'line':
Object.assign(node.style, {
margin: '0',
padding: '0',
});
break;
case 'syntax':
Object.assign(node.style, markdownStyle.syntax);
break;
case 'bold':
node.style.fontWeight = 'bold';
break;
case 'italic':
node.style.fontStyle = 'italic';
break;
case 'strikethrough':
node.style.textDecoration = 'line-through';
break;
case 'emoji':
Object.assign(node.style, {
...markdownStyle.emoji,
verticalAlign: 'middle',
fontStyle: 'normal', // remove italic
textDecoration: 'none', // remove strikethrough
display: 'inline-block',
});
break;
case 'mention-here':
Object.assign(node.style, markdownStyle.mentionHere);
break;
case 'mention-user':
Object.assign(node.style, markdownStyle.mentionUser);
break;
case 'mention-report':
Object.assign(node.style, markdownStyle.mentionReport);
break;
case 'link':
Object.assign(node.style, {
...markdownStyle.link,
textDecoration: 'underline',
});
break;
case 'code':
Object.assign(node.style, markdownStyle.code);
break;
case 'pre':
Object.assign(node.style, markdownStyle.pre);
break;
case 'blockquote':
Object.assign(node.style, {
...markdownStyle.blockquote,
borderLeftStyle: 'solid',
display: 'inline-block',
maxWidth: '100%',
boxSizing: 'border-box',
overflowWrap: 'anywhere',
});
break;
case 'h1':
Object.assign(node.style, {
...markdownStyle.h1,
fontWeight: 'bold',
});
break;
case 'block':
Object.assign(node.style, {
display: 'block',
margin: '0',
padding: '0',
position: 'relative',
});
break;
case 'text':
if (!isMultiline && targetElement.parentElement?.style) {
// Move text background styles from parent to the text node
const parentElement = targetElement.parentElement;
node.style.cssText = parentElement.style.cssText;
parentElement.style.cssText = '';
}
break;
default:
break;
}
}
const BLOCK_MARKDOWN_TYPES = ['inline-image'];
const FULL_LINE_MARKDOWN_TYPES = ['blockquote'];
function isBlockMarkdownType(type: NodeType) {
return BLOCK_MARKDOWN_TYPES.includes(type);
}
function getFirstBlockMarkdownRange(ranges: MarkdownRange[]) {
const blockMarkdownRange = ranges.find((r) => isBlockMarkdownType(r.type) || FULL_LINE_MARKDOWN_TYPES.includes(r.type));
return FULL_LINE_MARKDOWN_TYPES.includes(blockMarkdownRange?.type || '') ? undefined : blockMarkdownRange;
}
function extendBlockStructure(
currentInput: MarkdownTextInputElement,
targetNode: TreeNode,
currentRange: MarkdownRange,
ranges: MarkdownRange[],
text: string,
markdownStyle: PartialMarkdownStyle,
inlineImagesProps: InlineImagesInputProps,
) {
switch (currentRange.type) {
case 'inline-image':
return addInlineImagePreview(currentInput, targetNode, text, ranges, markdownStyle, inlineImagesProps);
default:
break;
}
return targetNode;
}
export {addStyleToBlock, extendBlockStructure, isBlockMarkdownType, getFirstBlockMarkdownRange};