/
util.ts
158 lines (140 loc) 路 4.63 KB
/
util.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { util, Graph, Point, FontStyle } from '@antv/x6'
export function autosize(graph: Graph) {
const cells = graph.getSelectedCells()
if (cells != null) {
graph.batchUpdate(() => {
cells.forEach(cell => {
if (graph.model.getChildCount(cell)) {
graph.updateGroupBounds([cell], 20)
} else {
const state = graph.view.getState(cell)
let geo = graph.getCellGeometry(cell)
if (
graph.model.isNode(cell) &&
state != null &&
state.text != null &&
geo != null &&
graph.isWrapping(cell)
) {
geo = geo.clone()
geo.bounds.height =
state.text.boundingBox!.height / graph.view.scale
graph.model.setGeometry(cell, geo)
} else {
graph.sizeManager.updateCellSize(cell)
}
}
})
})
}
}
export function wordWrap(graph: Graph) {
const cells = graph.getSelectedCells()
if (cells != null && cells.length) {
const style = graph.getStyle(cells[0])
const value = style.whiteSpace === 'nowrap' ? 'wrap' : 'nowrap'
graph.stopEditing()
graph.updateCellsStyle('whiteSpace', value, cells)
}
}
export function formattedText(graph: Graph) {
const cells = graph.getSelectedCells()
if (cells != null && cells.length > 0) {
graph.stopEditing()
graph.batchUpdate(() => {
cells.forEach(cell => {
const state = graph.view.getState(cell)
if (state != null) {
let label = graph.getLabel(state.cell)
if (typeof label === 'string') {
if (graph.isHtmlLabel(cell)) {
if (util.getBoolean(state.style, 'nl2Br', true)) {
label = label.replace(/\n/g, '').replace(/<br\s*.?>/g, '\n')
// Removes HTML tags
const temp = document.createElement('div')
temp.innerHTML = label
label = util.extractTextWithWhitespace(temp.childNodes as any)
}
} else {
// Converts HTML tags to text
label = util.escape(label)
if (util.getBoolean(state.style, 'nl2Br', true)) {
// Converts newlines in plain text to breaks in HTML
// to match the plain text output
label = label.replace(/\n/g, '<br/>')
}
}
graph.updateLabel(cell, label)
}
}
})
})
}
}
export function fitPage(graph: Graph, padding: Point = new Point()) {
if (!graph.pageVisible) {
// this.get('pageView').funct()
}
const fmt = graph.pageFormat
const ps = graph.pageScale
const cw = graph.container.clientWidth - 10
const ch = graph.container.clientHeight - 10
const scale =
Math.floor(20 * Math.min(cw / fmt.width / ps, ch / fmt.height / ps)) / 20
graph.zoomTo(scale)
if (util.hasScrollbars(graph.container)) {
graph.container.scrollTop = padding.y * graph.view.scale - 1
graph.container.scrollLeft =
Math.min(
padding.x * graph.view.scale,
(graph.container.scrollWidth - graph.container.clientWidth) / 2,
) - 1
}
}
export function fitTwoPages(graph: Graph, padding: Point = new Point()) {
if (!graph.pageVisible) {
// this.get('pageView').funct()
}
const fmt = graph.pageFormat
const ps = graph.pageScale
const cw = graph.container.clientWidth - 10
const ch = graph.container.clientHeight - 10
const scale =
Math.floor(20 * Math.min(cw / (2 * fmt.width) / ps, ch / fmt.height / ps)) /
20
graph.zoomTo(scale)
if (util.hasScrollbars(graph.container)) {
graph.container.scrollTop = Math.min(
padding.y,
(graph.container.scrollHeight - graph.container.clientHeight) / 2,
)
graph.container.scrollLeft = Math.min(
padding.x,
(graph.container.scrollWidth - graph.container.clientWidth) / 2,
)
}
}
export function fitPageWidth(graph: Graph, padding: Point = new Point()) {
if (!graph.pageVisible) {
// this.get('pageView').funct()
}
const fmt = graph.pageFormat
const ps = graph.pageScale
const cw = graph.container.clientWidth - 10
const scale = Math.floor((20 * cw) / fmt.width / ps) / 20
graph.zoomTo(scale)
if (util.hasScrollbars(graph.container)) {
graph.container.scrollLeft = Math.min(
padding.x * graph.view.scale,
(graph.container.scrollWidth - graph.container.clientWidth) / 2,
)
}
}
export function toggleFontStyle(graph: Graph, fontStyle: FontStyle) {
if (graph.isEditing()) {
document.execCommand(FontStyle[fontStyle], false)
} else {
graph.stopEditing(false)
graph.toggleCellsStyleFlag('fontStyle', fontStyle)
}
}