- Identify file, tokens, and rows:
- File:
- HTMLElement with which to count rows relative to
- Rows:
- Count row relative to File and store as configurable html property
- Set data attribute to save row number for quick reference and access
- i.e. "data-row=1"
- Tokens:
- Bind event handlers to individual token elements:
- onTokenEnter for when the cursor enters a token's HTMLElement
- onTokenLeave for when the cursor leaves a token's HTMLElement
- Count col start relative to Row and store as configurable html property
- can use .innerText to count character positions
- i.e. "data-col=0"
- Bind event handlers to individual token elements:
- File:
- onTokenEnter:
- Set off timer to debounce requests:
- Don't want to fire off a bunch of api calls as the users moves the cursour around the screen
- Calculate row and col:
- get colStart and colEnd from token data-col attribute
- colEnd can be calculated from colStart and innerText length
- get row from closest parental row's data-row attribute
- get colStart and colEnd from token data-col attribute
- if col & row are found
- Render a tooltip element:
- Relative to the token element itself
- Show tooltip loadings state:
- If there is no existing data for the token, show just a loading indicator
- If there is existing data for the token, show the previous data and a loading indicator
- Request tooltip content from API
- On success, save tooltip content to element scope in memory
- On success, render tooltip content
- On failure, show error message in tooltip
- Render a tooltip element:
- Set off timer to debounce requests:
interface AdditionalMarkupDescriptors {
fileTag?: string
rowTag?: string
tokenTag?: string
}
autocreateFiles({
fileTag = 'file',
rowTag = 'row',
tokenTag = 'token',
} = AdditionalMarkupDescriptors): void
createFile(file: HTMLElement, {
fileTag = 'file',
rowTag = 'row',
tokenTag = 'token',
} = AdditionalMarkupDescriptors): void
createRow(row: HTMLElement, {
rowTag = 'row',
} = AdditionalMarkupDescriptors): void
createToken(row: HTMLElement, {
tokenTag = 'token',
} = AdditionalMarkupDescriptors): void
Simplest example, using standard data markings:
<div data-file>
<div data-row="1">
<span data-col-start="1">console</span>
<span data-col-start="8">.</span>
<span data-col-start="9">log</span>
<span data-col-start="12">(</span>
<span data-col-start="13">"Hello World"</span>
<span data-col-start="26">)</span>
</div>
</div>
import {createFile} from 'hoverable-tooltips'
autocreateFiles({})
Custom example, using existing un-marked code with custom attributes
<file>
<row>
<module>console</module>
<accessor>.</accessor>
<attribute>log</attribute>
<invoke>(</invoke>
<string>"Hello World"</string>
<invoke>)</invoke>
</row>
</file>
import {createFile, createRow, createToken} from 'hoverable-tooltips'
const files = document.querySelectorAll('file')
files.forEach((file) => createFile(file, { fileTag: 'my-file' })
const rows = document.querySelectorAll('row')
rows.forEach((row) => createRow(row, { rowTag: 'my-row' })
const tokens = document.querySelectorAll('module, accessor, attribute, invoke, string')
tokens.forEach((token) => createToken(token, { tokenTag: 'my-token' })