Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional text expansion on mouseover #1

Merged
merged 10 commits into from
Feb 25, 2022
7 changes: 4 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ interface TelescopeNode {
depth: number;
telescopicOut: TelescopicOutput;
}
declare function _hydrate(line: Content, node: any): void;
declare function _createTelescopicText(content: Content[]): HTMLDivElement;
declare let lastTime: number;
declare function _hydrate(line: Content, node: any, hoverable?: boolean): void;
declare function _createTelescopicText(content: Content[], hoverable?: boolean): HTMLDivElement;
/*****************/
/*****************/
declare function _parseMarkdown(mdContent: string): TelescopicOutput;
Expand All @@ -31,4 +32,4 @@ declare function _parseMarkdownIntoContent(mdContent: string, separator?: string
* @param separator - character to divide items on the same indentation level.
* @returns HTML div containing the telescoping text.
*/
declare function createTelescopicTextFromBulletedList(listContent: string, separator?: string): HTMLDivElement;
declare function createTelescopicTextFromBulletedList(listContent: string, separator?: string, hoverable?: boolean): HTMLDivElement;
28 changes: 22 additions & 6 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ interface TelescopeNode {
telescopicOut: TelescopicOutput;
}

// time; recorded to prevent recursive text expansion on single hover
let _lastHoveredTime = Date.now();
spencerc99 marked this conversation as resolved.
Show resolved Hide resolved

// Internal recursive function to hydrate a node with a line object.
function _hydrate(line: Content, node: any) {
function _hydrate(line: Content, node: any, hoverable: boolean = false) {
let lineText = line.text;

if (line.replacements.length > 0) {
Expand Down Expand Up @@ -51,6 +54,18 @@ function _hydrate(line: Content, node: any) {
detail.classList.add("open");
});

// if the text is hoverable,
if (hoverable) {
// expand the text if text was not moused over immediately before
detail.addEventListener("mouseover", () => {
if (Date.now() - _lastHoveredTime > 10) {
detail.classList.remove("close");
detail.classList.add("open");
_lastHoveredTime = Date.now();
}
});
}

const summary = document.createElement("span");
summary.classList.add("summary");
summary.appendChild(document.createTextNode(replacement.og));
Expand All @@ -63,7 +78,7 @@ function _hydrate(line: Content, node: any) {
};
const expanded = document.createElement("span");
expanded.classList.add("expanded");
_hydrate(newLine, expanded);
_hydrate(newLine, expanded, hoverable);

// append to parent
detail.appendChild(expanded);
Expand Down Expand Up @@ -93,12 +108,12 @@ function _hydrate(line: Content, node: any) {

// Helper function to create a new `<div>` node containing the
// telescoping text.
function _createTelescopicText(content: Content[]) {
function _createTelescopicText(content: Content[], hoverable: boolean = false) {
const letter = document.createElement("div");
letter.id = "telescope";
content.forEach((line) => {
const newNode = document.createElement("p");
_hydrate(line, newNode);
_hydrate(line, newNode, hoverable);
letter.appendChild(newNode);
});
return letter;
Expand Down Expand Up @@ -225,8 +240,9 @@ function _parseMarkdownIntoContent(
*/
function createTelescopicTextFromBulletedList(
listContent: string,
separator: string = " "
separator: string = " ",
hoverable: boolean = false,
): HTMLDivElement {
const content = _parseMarkdownIntoContent(listContent, separator);
return _createTelescopicText([content]);
return _createTelescopicText([content], hoverable);
}