]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Added fade effect to page content highlighting
[bookstack] / resources / assets / js / pages / page-show.js
1 "use strict";
2 // Configure ZeroClipboard
3 const Clipboard = require("clipboard");
4
5 let setupPageShow = window.setupPageShow = function (pageId) {
6
7     // Set up pointer
8     let $pointer = $('#pointer').detach();
9     let pointerShowing = false;
10     let $pointerInner = $pointer.children('div.pointer').first();
11     let isSelection = false;
12     let pointerModeLink = true;
13     let pointerSectionId = '';
14
15     // Select all contents on input click
16     $pointer.on('click', 'input', function (e) {
17         $(this).select();
18         e.stopPropagation();
19     });
20
21     // Pointer mode toggle
22     $pointer.on('click', 'span.icon', event => {
23         let $icon = $(event.currentTarget);
24         pointerModeLink = !pointerModeLink;
25         $icon.html(pointerModeLink ? '<i class="zmdi zmdi-link"></i>' : '<i class="zmdi zmdi-square-down"></i>');
26         updatePointerContent();
27     });
28
29     // Set up clipboard
30     let clipboard = new Clipboard('#pointer button');
31
32     // Hide pointer when clicking away
33     $(document.body).find('*').on('click focus', event => {
34         if (!pointerShowing || isSelection) return;
35         let target = $(event.target);
36         if (target.is('.zmdi') || $(event.target).closest('#pointer').length === 1) return;
37
38         $pointer.detach();
39         pointerShowing = false;
40     });
41
42     function updatePointerContent() {
43         let inputText = pointerModeLink ? window.baseUrl(`/link/${pageId}#${pointerSectionId}`) : `{{@${pageId}#${pointerSectionId}}}`;
44         if (pointerModeLink && inputText.indexOf('http') !== 0) inputText = window.location.protocol + "//" + window.location.host + inputText;
45
46         $pointer.find('input').val(inputText);
47     }
48
49     // Show pointer when selecting a single block of tagged content
50     $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
51         e.stopPropagation();
52         let selection = window.getSelection();
53         if (selection.toString().length === 0) return;
54
55         // Show pointer and set link
56         let $elem = $(this);
57         pointerSectionId = $elem.attr('id');
58         updatePointerContent();
59
60         $elem.before($pointer);
61         $pointer.show();
62         pointerShowing = true;
63
64         // Set pointer to sit near mouse-up position
65         let pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
66         if (pointerLeftOffset < 0) pointerLeftOffset = 0;
67         let pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
68         $pointerInner.css('left', pointerLeftOffsetPercent + '%');
69
70         isSelection = true;
71         setTimeout(() => {
72             isSelection = false;
73         }, 100);
74     });
75
76     // Go to, and highlight if necessary, the specified text.
77     function goToText(text) {
78         let idElem = document.getElementById(text);
79         $('.page-content [data-highlighted]').attr('data-highlighted', '').css('background-color', '');
80         if (idElem !== null) {
81             let $idElem = $(idElem);
82             let color = $('#custom-styles').attr('data-color-light');
83             $idElem.css('background-color', color).attr('data-highlighted', 'true').smoothScrollTo();
84             setTimeout(() => {
85                 $idElem.addClass('anim').addClass('selectFade').css('background-color', '');
86                 setTimeout(() => {
87                    $idElem.removeClass('selectFade');
88                 }, 3000);
89             }, 100);
90         } else {
91             $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
92         }
93     }
94
95     // Check the hash on load
96     if (window.location.hash) {
97         let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
98         goToText(text);
99     }
100
101     // Sidebar page nav click event
102     $('.sidebar-page-nav').on('click', 'a', event => {
103         goToText(event.target.getAttribute('href').substr(1));
104     });
105
106     // Make the book-tree sidebar stick in view on scroll
107     let $window = $(window);
108     let $bookTree = $(".book-tree");
109     let $bookTreeParent = $bookTree.parent();
110     // Check the page is scrollable and the content is taller than the tree
111     let pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
112     // Get current tree's width and header height
113     let headerHeight = $("#header").height() + $(".toolbar").height();
114     let isFixed = $window.scrollTop() > headerHeight;
115     // Function to fix the tree as a sidebar
116     function stickTree() {
117         $bookTree.width($bookTreeParent.width() + 15);
118         $bookTree.addClass("fixed");
119         isFixed = true;
120     }
121     // Function to un-fix the tree back into position
122     function unstickTree() {
123         $bookTree.css('width', 'auto');
124         $bookTree.removeClass("fixed");
125         isFixed = false;
126     }
127     // Checks if the tree stickiness state should change
128     function checkTreeStickiness(skipCheck) {
129         let shouldBeFixed = $window.scrollTop() > headerHeight;
130         if (shouldBeFixed && (!isFixed || skipCheck)) {
131             stickTree();
132         } else if (!shouldBeFixed && (isFixed || skipCheck)) {
133             unstickTree();
134         }
135     }
136     // The event ran when the window scrolls
137     function windowScrollEvent() {
138         checkTreeStickiness(false);
139     }
140
141     // If the page is scrollable and the window is wide enough listen to scroll events
142     // and evaluate tree stickiness.
143     if (pageScrollable && $window.width() > 1000) {
144         $window.on('scroll', windowScrollEvent);
145         checkTreeStickiness(true);
146     }
147
148     // Handle window resizing and switch between desktop/mobile views
149     $window.on('resize', event => {
150         if (pageScrollable && $window.width() > 1000) {
151             $window.on('scroll', windowScrollEvent);
152             checkTreeStickiness(true);
153         } else {
154             $window.off('scroll', windowScrollEvent);
155             unstickTree();
156         }
157     });
158
159 };
160
161 module.exports = setupPageShow;