]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Updated page pointer to sit near mouse location and extracted page js into browserify...
[bookstack] / resources / assets / js / pages / page-show.js
1 "use strict";
2 // Configure ZeroClipboard
3 var zeroClipBoard = require('zeroclipboard');
4 zeroClipBoard.config({
5     swfPath: '/ZeroClipboard.swf'
6 });
7
8 window.setupPageShow = module.exports = function (pageId) {
9
10     // Set up pointer
11     var $pointer = $('#pointer').detach();
12     var $pointerInner = $pointer.children('div.pointer').first();
13     var isSelection = false;
14
15     // Select all contents on input click
16     $pointer.on('click', 'input', function(e) {
17         $(this).select();
18         e.stopPropagation();
19     });
20
21     // Set up copy-to-clipboard
22     new zeroClipBoard($pointer.find('button').first()[0]);
23
24     // Hide pointer when clicking away
25     $(document.body).find('*').on('click focus', function (e) {
26         if (!isSelection) {
27             $pointer.detach();
28         }
29     });
30
31     // Show pointer when selecting a single block of tagged content
32     $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
33         var selection = window.getSelection();
34         if (selection.toString().length === 0) return;
35
36         // Show pointer and set link
37         var $elem = $(this);
38         var link = window.location.protocol + "//" + window.location.host + '/link/' + pageId + '#' + $elem.attr('id');
39         $pointer.find('input').val(link);
40         $pointer.find('button').first().attr('data-clipboard-text', link);
41         $elem.before($pointer);
42         $pointer.show();
43
44         // Set pointer to sit near mouse-up position
45         var pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
46         if (pointerLeftOffset < 0) pointerLeftOffset = 0;
47         var pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
48         $pointerInner.css('left', pointerLeftOffsetPercent + '%');
49
50         e.stopPropagation();
51
52         isSelection = true;
53         setTimeout(() => {
54             isSelection = false;
55         }, 100);
56     });
57
58     // Go to, and highlight if necessary, the specified text.
59     function goToText(text) {
60         var idElem = $('.page-content #' + text).first();
61         if (idElem.length !== 0) {
62             idElem.smoothScrollTo();
63             idElem.css('background-color', 'rgba(244, 249, 54, 0.25)');
64         } else {
65             $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
66         }
67     }
68
69     // Check the hash on load
70     if (window.location.hash) {
71         var text = window.location.hash.replace(/\%20/g, ' ').substr(1);
72         goToText(text);
73     }
74
75 };