1 import * as DOM from '../services/dom';
2 import {Component} from './component';
3 import {copyTextToClipboard} from '../services/clipboard';
5 export class Pointer extends Component {
8 this.container = this.$el;
9 this.input = this.$refs.input;
10 this.button = this.$refs.button;
11 this.pageId = this.$opts.pageId;
15 this.isSelection = false;
16 this.pointerModeLink = true;
17 this.pointerSectionId = '';
19 this.setupListeners();
23 // Copy on copy button click
24 this.button.addEventListener('click', () => {
25 copyTextToClipboard(this.input.value);
28 // Select all contents on input click
29 this.input.addEventListener('click', event => {
31 event.stopPropagation();
34 // Prevent closing pointer when clicked or focused
35 DOM.onEvents(this.container, ['click', 'focus'], event => {
36 event.stopPropagation();
39 // Pointer mode toggle
40 DOM.onChildEvent(this.container, 'span.icon', 'click', (event, icon) => {
41 event.stopPropagation();
42 this.pointerModeLink = !this.pointerModeLink;
43 icon.querySelector('[data-icon="include"]').style.display = (!this.pointerModeLink) ? 'inline' : 'none';
44 icon.querySelector('[data-icon="link"]').style.display = (this.pointerModeLink) ? 'inline' : 'none';
45 this.updateForTarget();
48 // Hide pointer when clicking away
49 DOM.onEvents(document.body, ['click', 'focus'], () => {
50 if (!this.showing || this.isSelection) return;
54 // Show pointer when selecting a single block of tagged content
55 const pageContent = document.querySelector('.page-content');
56 DOM.onEvents(pageContent, ['mouseup', 'keyup'], event => {
57 event.stopPropagation();
58 const targetEl = event.target.closest('[id^="bkmrk"]');
60 this.showPointerAtTarget(targetEl, event.pageX);
66 this.container.style.display = null;
71 * Move and display the pointer at the given element, targeting the given screen x-position if possible.
72 * @param {Element} element
73 * @param {Number} xPosition
75 showPointerAtTarget(element, xPosition) {
76 const selection = window.getSelection();
77 if (selection.toString().length === 0) return;
79 // Show pointer and set link
80 this.pointerSectionId = element.id;
81 this.updateForTarget(element);
83 this.container.style.display = 'block';
84 const targetBounds = element.getBoundingClientRect();
85 const pointerBounds = this.container.getBoundingClientRect();
87 const xTarget = Math.min(Math.max(xPosition, targetBounds.left), targetBounds.right);
88 const xOffset = xTarget - (pointerBounds.width / 2);
89 const yOffset = (targetBounds.top - pointerBounds.height) - 16;
91 this.container.style.left = `${xOffset}px`;
92 this.container.style.top = `${yOffset}px`;
95 this.isSelection = true;
98 this.isSelection = false;
101 const scrollListener = () => {
103 window.removeEventListener('scroll', scrollListener, {passive: true});
105 window.addEventListener('scroll', scrollListener, {passive: true});
109 * Update the pointer inputs/content for the given target element.
110 * @param {?Element} element
112 updateForTarget(element) {
113 let inputText = this.pointerModeLink ? window.baseUrl(`/link/${this.pageId}#${this.pointerSectionId}`) : `{{@${this.pageId}#${this.pointerSectionId}}}`;
114 if (this.pointerModeLink && !inputText.startsWith('http')) {
115 inputText = `${window.location.protocol}//${window.location.host}${inputText}`;
118 this.input.value = inputText;
120 // Update anchor if present
121 const editAnchor = this.container.querySelector('#pointer-edit');
122 if (editAnchor && element) {
123 const {editHref} = editAnchor.dataset;
124 const elementId = element.id;
126 // get the first 50 characters.
127 const queryContent = element.textContent && element.textContent.substring(0, 50);
128 editAnchor.href = `${editHref}?content-id=${elementId}&content-text=${encodeURIComponent(queryContent)}`;