Skip to content

Commit

Permalink
update hint API
Browse files Browse the repository at this point in the history
  • Loading branch information
binrysearch committed Sep 8, 2024
1 parent dddc711 commit 83345c7
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 213 deletions.
24 changes: 17 additions & 7 deletions src/packages/hint/components/HintIcon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isFixed from "../../../util/isFixed";
import van from "../../dom/van";
import van, { State } from "../../dom/van";
import {
fixedHintClassName,
hideHintClassName,
Expand All @@ -17,6 +17,7 @@ const { a, div } = van.tags;
export type HintProps = {
index: number;
hintItem: HintItem;
refreshesSignal: State<number>;
onClick: (e: any) => void;
};

Expand All @@ -41,7 +42,12 @@ const className = (hintItem: HintItem) => {
const HintDot = () => div({ className: hintDotClassName });
const HintPulse = () => div({ className: hintPulseClassName });

export const HintIcon = ({ index, hintItem, onClick }: HintProps) => {
export const HintIcon = ({
index,
hintItem,
onClick,
refreshesSignal,
}: HintProps) => {
const hintElement = a(
{
[dataStepAttribute]: index.toString(),
Expand All @@ -54,11 +60,15 @@ export const HintIcon = ({ index, hintItem, onClick }: HintProps) => {
HintPulse()
);

alignHintPosition(
hintItem.hintPosition as HintPosition,
hintElement,
hintItem.element as HTMLElement
);
van.derive(() => {
if (refreshesSignal.val === undefined) return;

alignHintPosition(
hintItem.hintPosition as HintPosition,
hintElement,
hintItem.element as HTMLElement
);
});

return hintElement;
};
15 changes: 9 additions & 6 deletions src/packages/hint/components/HintsRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { hintsClassName } from "../className";
import { hideHint } from "../hide";
import { Hint } from "../hint";
import { HintItem } from "../hintItem";
import { showHintDialog } from "../tooltip";
import { HintIcon } from "./HintIcon";
import { ReferenceLayer } from "./ReferenceLayer";

Expand All @@ -27,7 +26,7 @@ const getHintClick = (hint: Hint, i: number) => (e: Event) => {
evt.cancelBubble = true;
}

showHintDialog(hint, i);
hint.showHintDialog(i);
};

export const HintsRoot = ({ hint }: HintsRootProps) => {
Expand All @@ -38,6 +37,7 @@ export const HintsRoot = ({ hint }: HintsRootProps) => {
index: i,
hintItem,
onClick: getHintClick(hint, i),
refreshesSignal: hint.getRefreshesSignal(),
});

// store the hint tooltip element in the hint item
Expand All @@ -55,20 +55,23 @@ export const HintsRoot = ({ hint }: HintsRootProps) => {
);

van.derive(() => {
if (hint._activeHintSignal.val === undefined) return;
const activeHintSignal = hint.getActiveHintSignal();
if (activeHintSignal.val === undefined) return;

const stepId = hint._activeHintSignal.val;
const stepId = activeHintSignal.val;
const hints = hint.getHints();
const hintItem = hints[stepId];

if (!hintItem) return;

const referenceLayer = ReferenceLayer({
activeHintSignal: hint._activeHintSignal,
activeHintSignal,
hintItem,

helperElementPadding: hint.getOption("helperElementPadding"),
targetElement: hint.getTargetElement(),

refreshes: hint._refreshes,
refreshes: hint.getRefreshesSignal(),

// hints don't have step numbers
showStepNumbers: false,
Expand Down
3 changes: 3 additions & 0 deletions src/packages/hint/hide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export async function hideHint(hint: Hint, hintItem: HintItem) {
if (isActiveSignal) {
isActiveSignal.val = false;
}

hint.hideHintDialog();

// call the callback function (if any)
hint.callback("hintClose")?.call(hint, hintItem);
}
Expand Down
111 changes: 90 additions & 21 deletions src/packages/hint/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import { fetchHintItems, HintItem } from "./hintItem";
import { setOption, setOptions } from "../../option";
import isFunction from "../../util/isFunction";
import debounce from "../../util/debounce";
import { reAlignHints } from "./position";
import DOMEvent from "../../util/DOMEvent";
import { getContainerElement } from "../../util/containerElement";
import { renderHints } from "./render";
import { hideHint, hideHints } from "./hide";
import { showHint, showHints } from "./show";
import { removeHint, removeHints } from "./remove";
import { showHintDialog } from "./tooltip";
import van from "../dom/van";
import { HintsRoot } from "./components/HintsRoot";

Expand All @@ -24,11 +20,8 @@ export class Hint implements Package<HintOptions> {
private _hints: HintItem[] = [];
private readonly _targetElement: HTMLElement;
private _options: HintOptions;
public _activeHintSignal = van.state<number | undefined>(undefined);
public _refreshes = van.state(0);

// The hint close function used when the user clicks outside the hint
private _windowClickHandler?: () => void;
private _activeHintSignal = van.state<number | undefined>(undefined);
private _refreshes = van.state(0);

private readonly callbacks: {
hintsAdded?: hintsAddedCallback;
Expand All @@ -38,6 +31,8 @@ export class Hint implements Package<HintOptions> {

// Event handlers
private _hintsAutoRefreshFunction?: () => void;
// The hint close function used when the user clicks outside the hint
private _windowClickFunction?: () => void;

/**
* Create a new Hint instance
Expand Down Expand Up @@ -110,7 +105,26 @@ export class Hint implements Package<HintOptions> {
return this;
}

public isRendered() {
/**
* Get the active hint signal
* This is meant to be used internally by the Hint package
*/
getActiveHintSignal() {
return this._activeHintSignal;
}

/**
* Returns the underlying state of the refreshes
* This is an internal method and should not be used outside of the package.
*/
getRefreshesSignal() {
return this._refreshes;
}

/**
* Returns true if the hints are rendered
*/
isRendered() {
return this._root !== undefined;
}

Expand All @@ -119,6 +133,13 @@ export class Hint implements Package<HintOptions> {
van.add(this._targetElement, this._root);
}

private recreateRoot() {
if (this._root) {
this._root.remove();
this.createRoot();
}
}

/**
* Render hints on the page
*/
Expand All @@ -132,16 +153,34 @@ export class Hint implements Package<HintOptions> {
}

fetchHintItems(this);
await renderHints(this);
this.createRoot();

this._windowClickHandler = () => {
this.callback("hintsAdded")?.call(this);

this.enableHintAutoRefresh();
this.enableCloseDialogOnWindowClick();

return this;
}

/**
* Enable closing the dialog when the user clicks outside the hint
*/
enableCloseDialogOnWindowClick() {
this._windowClickFunction = () => {
this._activeHintSignal.val = undefined;
};

DOMEvent.on(document, "click", this._windowClickHandler, false);
DOMEvent.on(document, "click", this._windowClickFunction, false);
}

return this;
/**
* Disable closing the dialog when the user clicks outside the hint
*/
disableCloseDialogOnWindowClick() {
if (this._windowClickFunction) {
DOMEvent.off(document, "click", this._windowClickFunction, false);
}
}

/**
Expand Down Expand Up @@ -205,11 +244,9 @@ export class Hint implements Package<HintOptions> {
this._root = undefined;
}

if (this._windowClickHandler) {
DOMEvent.off(document, "click", this._windowClickHandler, false);
}
this.disableHintAutoRefresh();
this.disableCloseDialogOnWindowClick();

removeHints(this);
return this;
}

Expand All @@ -229,7 +266,9 @@ export class Hint implements Package<HintOptions> {
* @param stepId The hint step ID
*/
removeHint(stepId: number) {
removeHint(stepId);
this._hints = this._hints.filter((_, i) => i !== stepId);
this.recreateRoot();

return this;
}

Expand All @@ -238,8 +277,38 @@ export class Hint implements Package<HintOptions> {
* @param stepId The hint step ID
*/
async showHintDialog(stepId: number) {
const item = this.getHint(stepId);

if (!item) return;

this._activeHintSignal.val = stepId;
await showHintDialog(this, stepId);

// call the callback function (if any)
await this.callback("hintClick")?.call(this, item);

return this;
}

/**
* Hide hint dialog from the page
*/
hideHintDialog() {
this._activeHintSignal.val = undefined;
return this;
}

/**
* Refresh the hints on the page
*/
public refresh() {
if (!this.isRendered()) {
return this;
}

if (this._refreshes.val !== undefined) {
this._refreshes.val += 1;
}

return this;
}

Expand All @@ -250,7 +319,7 @@ export class Hint implements Package<HintOptions> {
const hintAutoRefreshInterval = this.getOption("hintAutoRefreshInterval");
if (hintAutoRefreshInterval >= 0) {
this._hintsAutoRefreshFunction = debounce(
() => reAlignHints(this),
() => this.refresh(),
hintAutoRefreshInterval
);

Expand Down
12 changes: 0 additions & 12 deletions src/packages/hint/position.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import getOffset from "../../util/getOffset";
import { HintPosition } from "./hintItem";
import { Hint } from "./hint";

/**
* Aligns hint position
Expand Down Expand Up @@ -74,14 +73,3 @@ export const alignHintPosition = (
break;
}
};

/**
* Re-aligns all hint elements
*
* @api private
*/
export function reAlignHints(hint: Hint) {
for (const { hintTooltipElement, hintPosition, element } of hint.getHints()) {
alignHintPosition(hintPosition, element as HTMLElement, hintTooltipElement);
}
}
38 changes: 0 additions & 38 deletions src/packages/hint/remove.ts

This file was deleted.

Loading

0 comments on commit 83345c7

Please sign in to comment.