-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
192 lines (174 loc) · 6.17 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
Anchor,
WritingMode,
CssStyleSheet,
DomCallbackContext,
LogicalSize,
PhysicalSize,
ILogicalNode,
IFlowRootFormatContext,
} from 'nehan';
interface AnchorPos { left: number; top: number; }
function createPreviewContainerDOM(padding: number): HTMLElement {
const div = document.createElement("div");
div.style.padding = `${padding}px`;
div.style.border = "1px solid #dadada";
div.style.position = "absolute";
div.style.background = "white";
div.style.zIndex = "100";
return div;
}
function createAnchorTargetDOM(original: HTMLElement): HTMLElement {
const cloneDOM = original.cloneNode(true) as HTMLElement;
cloneDOM.style.left = "";
cloneDOM.style.top = "";
cloneDOM.style.right = "";
cloneDOM.style.bottom = "";
cloneDOM.style.position = "relative";
cloneDOM.style.color = "black";
return cloneDOM;
}
function getBodySize(flowRoot: IFlowRootFormatContext): PhysicalSize {
return new LogicalSize({
measure: flowRoot.maxMeasure,
extent: flowRoot.maxExtent
}).getPhysicalSize(flowRoot.env.writingMode);
}
function getLogicalNodeSize(node: ILogicalNode): PhysicalSize {
return new LogicalSize({
measure: node.measure,
extent: node.extent,
}).getPhysicalSize(node.env.writingMode);
}
function getPreviewSize(anchorTargetNode: ILogicalNode, spacing: number): PhysicalSize {
const size = getLogicalNodeSize(anchorTargetNode);
size.width += spacing;
size.height += spacing;
return size;
}
function isPreviewEnable(anchorLinkDOM: HTMLElement, previewContDOM: HTMLElement): boolean {
return anchorLinkDOM.contains(previewContDOM);
}
function deletePreview(anchorLinkDOM: HTMLElement, previewContDOM: HTMLElement) {
if (isPreviewEnable(anchorLinkDOM, previewContDOM)) {
anchorLinkDOM.removeChild(previewContDOM);
}
}
function getAnchorLinkPos(flowRoot: IFlowRootFormatContext, node: ILogicalNode): AnchorPos {
const bodySize = getBodySize(flowRoot);
const offsetPos = node.boxPos.offsetPos;
const mode = node.env.writingMode;
if (mode.isTextHorizontal()) {
return {
left: offsetPos.start,
top: offsetPos.before,
};
}
if (mode.isVerticalRl()) {
return {
// In vertical-rl, 'before' means "right: <before>px".
left: bodySize.width - offsetPos.before - node.extent,
top: offsetPos.start,
};
}
return {
left: offsetPos.before,
top: offsetPos.start,
};
}
function getPreviewPos(
writingMode: WritingMode,
bodySize: PhysicalSize,
anchorLinkPos: AnchorPos,
anchorLinkSize: PhysicalSize,
previewSize: PhysicalSize,
spacingSize: number): AnchorPos {
const ax = anchorLinkPos.left, ay = anchorLinkPos.top;
const aw = anchorLinkSize.width, ah = anchorLinkSize.height;
const pw = previewSize.width, ph = previewSize.height;
const bw = bodySize.width, bh = bodySize.height;
const ss = spacingSize;
let left = ax, top = ay;
if (writingMode.isTextHorizontal()) {
const rightOver = Math.max(0, ax + pw - bw);
const downOver = Math.max(0, ay + ah + ph - bh);
const upOver = Math.max(0, ph - ay);
left = (rightOver > 0) ? 0 : ax;
top = (downOver > upOver) ? -(ph + ss * 2) : (ah + ss);
} else {
const rightOver = Math.max(0, ax + aw + pw - bw);
const leftOver = Math.max(0, pw - ax);
const downOver = Math.max(0, ay + ph - bh);
// console.log(`downOver:${ay + ph - bh}, ay = ${ay}, ph = ${ph}, bh = ${bh}`);
if (writingMode.isVerticalRl()) {
left = (leftOver > rightOver) ? aw + ss : -(pw + 2 * ss);
} else {
left = (rightOver > leftOver) ? -(pw + 2 * ss) : aw + ss;
}
top = (downOver > 0) ? 0 : ay;
}
return { left, top };
}
export function create(args: {
previewSpacing: number;
onClickAnchorLink: (anchor: Anchor) => void;
}): CssStyleSheet {
return new CssStyleSheet({
"a[href^='#']": {
"@create": (ctx: DomCallbackContext) => {
// console.log("@create anchor:", ctx);
const href = ctx.box.env.element.getAttribute("href") || "";
const anchorName = href.substring(1);
const anchorLinkDOM = ctx.dom;
const previewContDOM = createPreviewContainerDOM(args.previewSpacing);
const writingMode = ctx.box.env.writingMode;
const bodySize = getBodySize(ctx.flowRoot);
// console.log("create anchor(%s), target:%o", href, anchor);
anchorLinkDOM.addEventListener("click", (e: Event) => {
e.preventDefault();
const anchor = ctx.flowRoot.getAnchor(anchorName);
if (!anchor) {
return false;
}
deletePreview(anchorLinkDOM, previewContDOM);
args.onClickAnchorLink(anchor);
// console.log("click:%o,%o", e, ctx);
return false;
});
anchorLinkDOM.addEventListener("setpage", (e: Event) => {
deletePreview(anchorLinkDOM, previewContDOM);
});
anchorLinkDOM.onmouseover = (e) => {
if (isPreviewEnable(anchorLinkDOM, previewContDOM)) {
return;
}
const anchor = ctx.flowRoot.getAnchor(anchorName);
if (!anchor || !anchor.box) {
return;
}
if (anchor.box.extent === 0) {
return;
}
if (anchor.box && !anchor.dom) {
const etor = ctx.flowRoot.createLogicalNodeEvaluator();
anchor.dom = anchor.box.acceptEvaluator(etor) as HTMLElement;
}
if (anchor.dom && previewContDOM.childElementCount === 0) {
const anchorTargetDOM = createAnchorTargetDOM(anchor.dom);
previewContDOM.appendChild(anchorTargetDOM);
}
const anchorLinkPos = getAnchorLinkPos(ctx.flowRoot, ctx.box);
const anchorLinkSize = getLogicalNodeSize(ctx.box);
const previewSize = getPreviewSize(anchor.box, args.previewSpacing);
const previewPos = getPreviewPos(writingMode, bodySize, anchorLinkPos, anchorLinkSize, previewSize, args.previewSpacing);
previewContDOM.style.left = `${previewPos.left}px`;
previewContDOM.style.top = `${previewPos.top}px`;
anchorLinkDOM.appendChild(previewContDOM);
};
anchorLinkDOM.onmouseout = (e) => {
deletePreview(anchorLinkDOM, previewContDOM);
};
},
}
});
}