-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
53 lines (48 loc) · 1.63 KB
/
index.ts
File metadata and controls
53 lines (48 loc) · 1.63 KB
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
import TreeLayout from './layout/base';
import indentedTree from './layout/indented';
import separateTree from './layout/separate-root';
import util from './layout/util';
const VALID_DIRECTIONS = [
'LR', // left to right
'RL', // right to left
'H', // horizontal
];
const DEFAULT_DIRECTION = VALID_DIRECTIONS[0];
class IndentedLayout extends TreeLayout {
execute() {
const me = this;
const options = me.options;
const root = me.rootNode;
options.isHorizontal = true;
// default indent 20 and sink first children;
const { indent = 20, dropCap = true, direction = DEFAULT_DIRECTION, align } = options;
if (direction && VALID_DIRECTIONS.indexOf(direction) === -1) {
throw new TypeError(`Invalid direction: ${direction}`);
}
if (direction === VALID_DIRECTIONS[0]) {
// LR
indentedTree(root, indent, dropCap, align);
} else if (direction === VALID_DIRECTIONS[1]) {
// RL
indentedTree(root, indent, dropCap, align);
root.right2left();
} else if (direction === VALID_DIRECTIONS[2]) {
// H
// separate into left and right trees
const { left, right } = separateTree(root, options);
indentedTree(left, indent, dropCap, align);
left.right2left();
indentedTree(right, indent, dropCap, align);
const bbox = left.getBoundingBox();
right.translate(bbox.width, 0);
root.x = right.x - root.width / 2;
}
return root;
}
}
const DEFAULT_OPTIONS = {};
function indentedLayout(root, options) {
options = util.assign({}, DEFAULT_OPTIONS, options);
return new IndentedLayout(root, options).execute();
}
export default indentedLayout;