2022-09-14 15:31:16 +03:00
|
|
|
import copyToClipboard from '../utils/copyToClipboard';
|
|
|
|
|
2018-10-20 17:54:15 +03:00
|
|
|
/**
|
|
|
|
* @class Page
|
|
|
|
* @classdesc Class for page module
|
|
|
|
*/
|
2022-07-26 18:49:30 +03:00
|
|
|
export default class Page {
|
2018-10-20 17:54:15 +03:00
|
|
|
/**
|
|
|
|
* Creates base properties
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
this.codeStyler = null;
|
2022-07-26 18:49:30 +03:00
|
|
|
this.tableOfContent = null;
|
2018-10-20 17:54:15 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:31:16 +03:00
|
|
|
/**
|
|
|
|
* CSS classes used in the codes
|
|
|
|
*
|
|
|
|
* @returns {Record<string, string>}
|
|
|
|
*/
|
|
|
|
static get CSS() {
|
|
|
|
return {
|
|
|
|
page: 'page',
|
|
|
|
copyLinkBtn: 'block-header__copy-link',
|
|
|
|
header: 'block-header--anchor',
|
|
|
|
headerLinkCopied: 'block-header--link-copied',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:54:15 +03:00
|
|
|
/**
|
|
|
|
* Called by ModuleDispatcher to initialize module from DOM
|
|
|
|
*/
|
2022-07-26 18:49:30 +03:00
|
|
|
init() {
|
2018-10-20 17:54:15 +03:00
|
|
|
this.codeStyler = this.createCodeStyling();
|
2022-07-26 18:49:30 +03:00
|
|
|
this.tableOfContent = this.createTableOfContent();
|
2022-09-14 15:31:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add click event listener to capture copy link button clicks
|
|
|
|
*/
|
|
|
|
const page = document.querySelector(`.${Page.CSS.page}`);
|
|
|
|
|
|
|
|
page.addEventListener('click', this.copyAnchorLinkIfNeeded);
|
2022-07-26 18:49:30 +03:00
|
|
|
}
|
2018-10-20 17:54:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init code highlighting
|
|
|
|
*/
|
|
|
|
async createCodeStyling() {
|
Authentication (#22)
* Authorization added
* added secret to password, md5 hashing, removed promise from verifyToken, deleted links when not authorized
* added dbinsert script
* turned verifyToken to middleware, added description for dbinsert, added hidden csrf field in auth form
* added middlewares, user model and controller
* JSDoc fix
* wrong password processing fix
* added comments to dbinsert script, moved salt and passHash to singe db doc
* Moved salt to .env, upgradedscript for generating password was, fixed comments and JSDoc
* Deleted using salt (now user is only one), changed verifying password to bcrypt.compare, added httpyOnly property to jwt cookie
2019-03-06 13:22:57 +03:00
|
|
|
const { default: CodeStyler } = await import(/* webpackChunkName: "code-styling" */ './../classes/codeStyler');
|
2018-10-20 17:54:15 +03:00
|
|
|
|
2022-07-26 18:49:30 +03:00
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-new
|
|
|
|
new CodeStyler({
|
|
|
|
selector: '.block-code__content',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error); // @todo send to Hawk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init table of content
|
|
|
|
*
|
|
|
|
* @returns {Promise<TableOfContent>}
|
|
|
|
*/
|
|
|
|
async createTableOfContent() {
|
|
|
|
const { default: TableOfContent } = await import(/* webpackChunkName: "table-of-content" */ '../classes/table-of-content');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-new
|
|
|
|
new TableOfContent({
|
|
|
|
tagSelector:
|
|
|
|
'h2.block-header--anchor,' +
|
|
|
|
'h3.block-header--anchor,' +
|
|
|
|
'h4.block-header--anchor',
|
|
|
|
appendTo: document.getElementById('layout-sidebar-right'),
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error); // @todo send to Hawk
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 15:31:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if 'copy link' button was clicked and copies the link to clipboard
|
|
|
|
*
|
|
|
|
* @param e - click event
|
|
|
|
*/
|
|
|
|
copyAnchorLinkIfNeeded = async (e) => {
|
|
|
|
const copyLinkButtonClicked = e.target.closest(`.${Page.CSS.copyLinkBtn}`);
|
|
|
|
|
|
|
|
if (!copyLinkButtonClicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const header = e.target.closest(`.${Page.CSS.header}`);
|
|
|
|
const link = header.querySelector('a').href;
|
|
|
|
|
|
|
|
await copyToClipboard(link);
|
|
|
|
header.classList.add(Page.CSS.headerLinkCopied);
|
|
|
|
|
|
|
|
header.addEventListener('mouseleave', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
header.classList.remove(Page.CSS.headerLinkCopied);
|
|
|
|
}, 500);
|
|
|
|
}, { once: true });
|
|
|
|
}
|
2018-10-20 17:54:15 +03:00
|
|
|
}
|