-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathpointer.ts
More file actions
206 lines (171 loc) · 7.79 KB
/
Copy pathpointer.ts
File metadata and controls
206 lines (171 loc) · 7.79 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as DOM from '../services/dom';
import {Component} from './component';
import {copyTextToClipboard} from '../services/clipboard';
import {hashElement, normalizeNodeTextOffsetToParent} from "../services/dom";
import {PageComments} from "./page-comments";
export class Pointer extends Component {
protected showing: boolean = false;
protected isMakingSelection: boolean = false;
protected targetElement: HTMLElement|null = null;
protected targetSelectionRange: Range|null = null;
protected pointer!: HTMLElement;
protected linkInput!: HTMLInputElement;
protected linkButton!: HTMLElement;
protected includeInput!: HTMLInputElement;
protected includeButton!: HTMLElement;
protected sectionModeButton!: HTMLElement;
protected commentButton!: HTMLElement;
protected modeToggles!: HTMLElement[];
protected modeSections!: HTMLElement[];
protected pageId!: string;
setup() {
this.pointer = this.$refs.pointer;
this.linkInput = this.$refs.linkInput as HTMLInputElement;
this.linkButton = this.$refs.linkButton;
this.includeInput = this.$refs.includeInput as HTMLInputElement;
this.includeButton = this.$refs.includeButton;
this.sectionModeButton = this.$refs.sectionModeButton;
this.commentButton = this.$refs.commentButton;
this.modeToggles = this.$manyRefs.modeToggle;
this.modeSections = this.$manyRefs.modeSection;
this.pageId = this.$opts.pageId;
this.setupListeners();
}
setupListeners() {
// Copy on copy button click
this.includeButton.addEventListener('click', () => copyTextToClipboard(this.includeInput.value));
this.linkButton.addEventListener('click', () => copyTextToClipboard(this.linkInput.value));
// Select all contents on input click
DOM.onSelect([this.includeInput, this.linkInput], event => {
(event.target as HTMLInputElement).select();
event.stopPropagation();
});
// Prevent closing pointer when clicked or focused
DOM.onEvents(this.pointer, ['click', 'focus'], event => {
event.stopPropagation();
});
// Hide pointer when clicking away
DOM.onEvents(document.body, ['click', 'focus'], () => {
if (!this.showing || this.isMakingSelection) return;
this.hidePointer();
});
// Hide pointer on escape press
DOM.onEscapePress(this.pointer, this.hidePointer.bind(this));
// Show pointer when selecting a single block of tagged content
const pageContent = document.querySelector('.page-content');
DOM.onEvents(pageContent, ['mouseup', 'keyup'], event => {
event.stopPropagation();
const targetEl = (event.target as HTMLElement).closest('[id^="bkmrk"]');
if (targetEl instanceof HTMLElement && (window.getSelection() || '').toString().length > 0) {
const xPos = (event instanceof MouseEvent) ? event.pageX : 0;
this.showPointerAtTarget(targetEl, xPos, false);
}
});
// Start section selection mode on button press
DOM.onSelect(this.sectionModeButton, this.enterSectionSelectMode.bind(this));
// Toggle between pointer modes
DOM.onSelect(this.modeToggles, event => {
const targetToggle = (event.target as HTMLElement);
for (const section of this.modeSections) {
const show = !section.contains(targetToggle);
section.toggleAttribute('hidden', !show);
}
const otherToggle = this.modeToggles.find(b => b !== targetToggle);
otherToggle && otherToggle.focus();
});
if (this.commentButton) {
DOM.onSelect(this.commentButton, this.createCommentAtPointer.bind(this));
}
}
hidePointer() {
this.pointer.style.removeProperty('display');
this.showing = false;
this.targetElement = null;
this.targetSelectionRange = null;
}
/**
* Move and display the pointer at the given element, targeting the given screen x-position if possible.
*/
showPointerAtTarget(element: HTMLElement, xPosition: number, keyboardMode: boolean) {
this.targetElement = element;
this.targetSelectionRange = window.getSelection()?.getRangeAt(0) || null;
this.updateDomForTarget(element);
this.pointer.style.display = 'block';
const targetBounds = element.getBoundingClientRect();
const pointerBounds = this.pointer.getBoundingClientRect();
const xTarget = Math.min(Math.max(xPosition, targetBounds.left), targetBounds.right);
const xOffset = xTarget - (pointerBounds.width / 2);
const yOffset = (targetBounds.top - pointerBounds.height) - 16;
this.pointer.style.left = `${xOffset}px`;
this.pointer.style.top = `${yOffset}px`;
this.showing = true;
this.isMakingSelection = true;
setTimeout(() => {
this.isMakingSelection = false;
}, 100);
const scrollListener = () => {
this.hidePointer();
window.removeEventListener('scroll', scrollListener);
};
element.parentElement?.insertBefore(this.pointer, element);
if (!keyboardMode) {
window.addEventListener('scroll', scrollListener, {passive: true});
}
}
/**
* Update the pointer inputs/content for the given target element.
*/
updateDomForTarget(element: HTMLElement) {
const permaLink = window.baseUrl(`/link/${this.pageId}#${element.id}`);
const includeTag = `{{@${this.pageId}#${element.id}}}`;
this.linkInput.value = permaLink;
this.includeInput.value = includeTag;
// Update anchor if present
const editAnchor = this.pointer.querySelector('#pointer-edit');
if (editAnchor instanceof HTMLAnchorElement && element) {
const {editHref} = editAnchor.dataset;
const elementId = element.id;
// Get the first 50 characters.
const queryContent = (element.textContent || '').substring(0, 50);
editAnchor.href = `${editHref}?content-id=${elementId}&content-text=${encodeURIComponent(queryContent)}`;
}
}
enterSectionSelectMode() {
const sections = Array.from(document.querySelectorAll('.page-content [id^="bkmrk"]')) as HTMLElement[];
for (const section of sections) {
section.setAttribute('tabindex', '0');
}
sections[0].focus();
DOM.onEnterPress(sections, event => {
this.showPointerAtTarget(event.target as HTMLElement, 0, true);
this.pointer.focus();
});
}
createCommentAtPointer() {
if (!this.targetElement) {
return;
}
const refId = this.targetElement.id;
const hash = hashElement(this.targetElement);
let range = '';
if (this.targetSelectionRange) {
const commonContainer = this.targetSelectionRange.commonAncestorContainer;
if (this.targetElement.contains(commonContainer)) {
const start = normalizeNodeTextOffsetToParent(
this.targetSelectionRange.startContainer,
this.targetSelectionRange.startOffset,
this.targetElement
);
const end = normalizeNodeTextOffsetToParent(
this.targetSelectionRange.endContainer,
this.targetSelectionRange.endOffset,
this.targetElement
);
range = `${start}-${end}`;
}
}
const reference = `${refId}:${hash}:${range}`;
const pageComments = window.$components.first('page-comments') as PageComments;
pageComments.startNewComment(reference);
}
}