Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add hint for highlight target element (#1740)
boojack01774e3a78f7
diff --git a/frontend/src/plugins/demo/data.ts b/frontend/src/plugins/demo/data.tsindex 9a5ce26d73..8010117171 100644--- a/frontend/src/plugins/demo/data.ts+++ b/frontend/src/plugins/demo/data.ts@@ -1,6 +1,6 @@import { merge } from "lodash-es";import { validateStepData } from "./guide";-import { GuideData } from "./types";+import { GuideData, HintData } from "./types";const fetchJSONData = async (path: string) => {const res = await fetch("/static/demo" + path);@@ -15,3 +15,10 @@ export const fetchGuideDataWithName = async (guideName: string) => {guideData.steps = guideData.steps.filter((s) => validateStepData(s));return guideData;};++export const fetchHintDataWithName = async (hintName: string) => {+ const hintData = (await fetchJSONData(+ `/${hintName}/hint.json`+ )) as HintData[];+ return hintData;+};diff --git a/frontend/src/plugins/demo/guide.ts b/frontend/src/plugins/demo/guide.tsindex d7f17ee0cb..aed9b87479 100644--- a/frontend/src/plugins/demo/guide.ts+++ b/frontend/src/plugins/demo/guide.ts@@ -7,7 +7,7 @@ import {waitForTargetElement,} from "./utils";import * as storage from "./storage";-import { GuideData, GuidePosition, StepData } from "./types";+import { DialogPosition, GuideData, StepData } from "./types";import { getStringFromI18NText, nextI18NText } from "./i18n";// validateStepData will check if the step data is valid@@ -49,7 +49,7 @@ export const showGuideDialog = async (removeGuideDialog();renderHighlight(targetElement, guideStep);renderGuideDialog(targetElement, guideData, stepIndex);- requestAnimationFrame(() => updateGuidePosition(targetElement, guideStep));+ requestAnimationFrame(() => updateDialogPosition(targetElement, guideStep));}};@@ -162,7 +162,7 @@ const renderGuideDialog = (const adjustGuideDialogPosition = (targetElement: HTMLElement,guideDialogDiv: HTMLElement,- position: GuidePosition = "bottom"+ position: DialogPosition = "bottom") => {const bounding = getElementBounding(targetElement);const guideDialogBounding = getElementBounding(guideDialogDiv);@@ -186,7 +186,7 @@ const adjustGuideDialogPosition = (}};-const updateGuidePosition = (+const updateDialogPosition = (targetElement: HTMLElement,guideStep: StepData) => {@@ -210,7 +210,7 @@ const updateGuidePosition = (adjustGuideDialogPosition(targetElement, guideDialogDiv, guideStep.position);- requestAnimationFrame(() => updateGuidePosition(targetElement, guideStep));+ requestAnimationFrame(() => updateDialogPosition(targetElement, guideStep));};export const removeGuideDialog = () => {diff --git a/frontend/src/plugins/demo/hint.ts b/frontend/src/plugins/demo/hint.tsindex 712916a3bf..1ad8a8a5eb 100644--- a/frontend/src/plugins/demo/hint.ts+++ b/frontend/src/plugins/demo/hint.ts@@ -1,2 +1,209 @@-// Hint is a special guide that has no Next button and is always shown.-export default null;+import { indexOf, isUndefined } from "lodash-es";+import { getStringFromI18NText } from "./i18n";+import { DialogPosition, HintData } from "./types";+import {+ checkUrlPathnameMatched,+ getElementBounding,+ getElementMaxZIndex,+ waitForTargetElement,+} from "./utils";++const closedHintIndexSet = new Set<number>();++export const showHints = async (hintDataList: HintData[]) => {+ removeHint();+ for (const hintData of hintDataList) {+ const index = indexOf(hintDataList, hintData);+ if (+ closedHintIndexSet.has(index) ||+ (hintData.url && !checkUrlPathnameMatched(hintData.url))+ ) {+ continue;+ }++ const targetElement = await waitForTargetElement([[hintData.selector]]);+ if (targetElement) {+ removeHint(index);+ renderHighlight(targetElement, hintData, index);+ updateHintPosition(targetElement, hintData, index);+ }+ }+};++const renderHighlight = (+ targetElement: HTMLElement,+ hintData: HintData,+ hintIndex: number+) => {+ const highlightWrapper = document.createElement("div");+ highlightWrapper.className = `bb-hint-highlight-wrapper bb-hint-${hintIndex}`;+ document.body.appendChild(highlightWrapper);++ const maxZIndex = getElementMaxZIndex(targetElement);+ highlightWrapper.style.zIndex = `${maxZIndex}`;++ if (hintData.additionStyle) {+ for (const key in hintData.additionStyle) {+ highlightWrapper.style[key] = hintData.additionStyle[key];+ }+ }++ if (!checkShouldShowDialog(hintData)) {+ highlightWrapper.style.pointerEvents = "none";+ }++ const bounding = getElementBounding(targetElement);+ highlightWrapper.style.top = `${bounding.top}px`;+ highlightWrapper.style.left = `${bounding.left}px`;+ highlightWrapper.style.width = `${bounding.width}px`;+ highlightWrapper.style.height = `${bounding.height}px`;++ highlightWrapper.addEventListener(+ "click",+ (e) => {+ e.stopPropagation();+ highlightWrapper.style.pointerEvents = "none";+ renderHintDialog(targetElement, hintData, hintIndex);+ },+ {+ once: true,+ }+ );+};++const renderHintDialog = (+ targetElement: HTMLElement,+ hintData: HintData,+ hintIndex: number+) => {+ const { title, description } = hintData;+ const hintDialogDiv = document.createElement("div");+ hintDialogDiv.className = `bb-hint-dialog bb-hint-dialog-${hintIndex}`;++ const maxZIndex = getElementMaxZIndex(targetElement);+ hintDialogDiv.style.zIndex = `${maxZIndex}`;++ if (hintData.additionStyle) {+ for (const key in hintData.additionStyle) {+ hintDialogDiv.style[key] = hintData.additionStyle[key];+ }+ }++ const closeButton = document.createElement("button");+ closeButton.className = "bb-hint-close-button";+ closeButton.innerHTML = "×";+ closeButton.addEventListener("click", () => {+ removeHint(hintIndex);+ closedHintIndexSet.add(hintIndex);+ });+ hintDialogDiv.appendChild(closeButton);++ if (getStringFromI18NText(title)) {+ const titleElement = document.createElement("p");+ titleElement.className = "bb-hint-title-text";+ titleElement.innerText = getStringFromI18NText(title);+ hintDialogDiv.appendChild(titleElement);+ }+ if (getStringFromI18NText(description)) {+ const descriptionElement = document.createElement("p");+ descriptionElement.className = "bb-hint-description-text";+ descriptionElement.innerText = getStringFromI18NText(description);+ hintDialogDiv.appendChild(descriptionElement);+ }++ document.body.appendChild(hintDialogDiv);+ adjustHintDialogPosition(targetElement, hintDialogDiv, hintData.position);+};++const adjustHintDialogPosition = (+ targetElement: HTMLElement,+ hintDialogDiv: HTMLElement,+ position: DialogPosition = "bottom"+) => {+ const bounding = getElementBounding(targetElement);+ const guideDialogBounding = getElementBounding(hintDialogDiv);++ if (position === "bottom") {+ hintDialogDiv.style.top = `${bounding.top + bounding.height}px`;+ hintDialogDiv.style.left = `${bounding.left - 4}px`;+ } else if (position === "top") {+ hintDialogDiv.style.top = `${+ bounding.top - guideDialogBounding.height - 8+ }px`;+ hintDialogDiv.style.left = `${bounding.left - 4}px`;+ } else if (position === "left") {+ hintDialogDiv.style.top = `${bounding.top - 4}px`;+ hintDialogDiv.style.left = `${+ bounding.left - guideDialogBounding.width - 8+ }px`;+ } else if (position === "right") {+ hintDialogDiv.style.top = `${bounding.top - 4}px`;+ hintDialogDiv.style.left = `${bounding.left + bounding.width}px`;+ }+};++const updateHintPosition = (+ targetElement: HTMLElement,+ hintData: HintData,+ hintIndex: number+) => {+ const highlightWrapper = document.body.querySelector(+ `.bb-hint-highlight-wrapper.bb-hint-${hintIndex}`+ ) as HTMLElement;++ if (!targetElement || !highlightWrapper) {+ removeHint(hintIndex);+ return;+ }++ const bounding = getElementBounding(targetElement);++ highlightWrapper.style.top = `${bounding.top}px`;+ highlightWrapper.style.left = `${bounding.left}px`;+ highlightWrapper.style.width = `${bounding.width}px`;+ highlightWrapper.style.height = `${bounding.height}px`;++ requestAnimationFrame(() =>+ updateHintPosition(targetElement, hintData, hintIndex)+ );+};++export const removeHint = (hintIndex?: number) => {+ if (isUndefined(hintIndex)) {+ const hintWrappers = document.querySelectorAll(+ ".bb-hint-highlight-wrapper"+ );+ if (hintWrappers) {+ hintWrappers.forEach((hintWrapper) => {+ hintWrapper.remove();+ });+ }+ const hintDialogs = document.querySelectorAll(".bb-hint-dialog");+ if (hintDialogs) {+ hintDialogs.forEach((hintDialog) => {+ hintDialog.remove();+ });+ }+ } else {+ const hintWrapper = document.querySelector(+ `.bb-hint-highlight-wrapper.bb-hint-${hintIndex}`+ );+ if (hintWrapper) {+ hintWrapper.remove();+ }+ const hintDialog = document.querySelector(+ `.bb-hint-dialog.bb-hint-dialog-${hintIndex}`+ );+ if (hintDialog) {+ hintDialog.remove();+ }+ }+};++const checkShouldShowDialog = (hintData: HintData): boolean => {+ if (!isUndefined(hintData.title)) {+ return true;+ }++ return false;+};diff --git a/frontend/src/plugins/demo/index.ts b/frontend/src/plugins/demo/index.tsindex ed49e655f1..78029e93e3 100644--- a/frontend/src/plugins/demo/index.ts+++ b/frontend/src/plugins/demo/index.ts@@ -1,17 +1,24 @@-import initGuideListeners from "./listener";+import { initGuideListeners, initHintListeners } from "./listener";import * as storage from "./storage";// initial guide listeners when window loadedwindow.addEventListener("load",() => {+ const { guide: storageGuide, hint: storageHint } = storage.get([+ "guide",+ "hint",+ ]);const params = new URLSearchParams(window.location.search);const paramGuide = params.get("guide");- const { guide: storageGuide } = storage.get(["guide"]);+ const paramHint = params.get("hint");if (paramGuide || storageGuide) {initGuideListeners();}+ if (paramHint || storageHint) {+ initHintListeners();+ }},{once: true,diff --git a/frontend/src/plugins/demo/listener.ts b/frontend/src/plugins/demo/listener.tsindex 56c07ed24f..a6f6b15f76 100644--- a/frontend/src/plugins/demo/listener.ts+++ b/frontend/src/plugins/demo/listener.ts@@ -1,6 +1,7 @@import { isEqual } from "lodash-es";-import { fetchGuideDataWithName } from "./data";+import { fetchGuideDataWithName, fetchHintDataWithName } from "./data";import { removeGuideDialog, showGuideDialog } from "./guide";+import { removeHint, showHints } from "./hint";import * as storage from "./storage";const refreshLocalStorageGuideData = async () => {@@ -22,6 +23,23 @@ const refreshLocalStorageGuideData = async () => {}};+const refreshLocalStorageHintData = async () => {+ const params = new URLSearchParams(window.location.search);+ const hintName = params.get("hint");++ if (hintName) {+ const hintData = await fetchHintDataWithName(hintName);++ if (hintData) {+ storage.set({+ hint: {+ name: hintName,+ },+ });+ }+ }+};+const tryToShowGuideDialog = async () => {const { guide } = storage.get(["guide"]);@@ -33,8 +51,20 @@ const tryToShowGuideDialog = async () => {}};-// initLocationListener using mutation observer to detect DOM changes for mocking location changed.-const initLocationListener = () => {+const tryToShowHints = async () => {+ removeHint();+ const { hint } = storage.get(["hint"]);++ if (hint) {+ const hintData = await fetchHintDataWithName(hint.name);+ showHints(hintData);+ } else {+ removeHint();+ }+};++// initLocationListenerForGuide using mutation observer to detect DOM changes for mocking location changed.+const initLocationListenerForGuide = () => {let prevLocationString = "";const observer = new MutationObserver(async () => {@@ -47,22 +77,47 @@ const initLocationListener = () => {}});- observer.observe(document.querySelector("body") as HTMLBodyElement, {+ observer.observe(document.body, {childList: true,subtree: true,});};-// initStorageListener detecting storage changes-const initStorageListener = () => {+// initMutationListenerForHint using mutation observer to detect DOM changes.+const initMutationListenerForHint = () => {+ const observer = new MutationObserver(async (mutations: MutationRecord[]) => {+ for (const mutation of mutations) {+ const changedNodes = [...mutation.addedNodes, ...mutation.removedNodes];+ for (const changedNode of changedNodes) {+ if (+ changedNode instanceof HTMLElement &&+ !changedNode.matches(".bb-hint-highlight-wrapper") &&+ !changedNode.matches(".bb-hint-dialog")+ ) {+ tryToShowHints();+ return;+ }+ }+ }+ });++ observer.observe(document.body, {+ childList: true,+ subtree: true,+ });+};++export const initGuideListeners = () => {+ initLocationListenerForGuide();window.addEventListener("storage", async () => {tryToShowGuideDialog();});};-const initGuideListeners = () => {- initLocationListener();- initStorageListener();+export const initHintListeners = async () => {+ await refreshLocalStorageHintData();+ initMutationListenerForHint();+ window.addEventListener("storage", async () => {+ tryToShowHints();+ });};--export default initGuideListeners;diff --git a/frontend/src/plugins/demo/storage.ts b/frontend/src/plugins/demo/storage.tsindex 9f81454045..b684784abd 100644--- a/frontend/src/plugins/demo/storage.ts+++ b/frontend/src/plugins/demo/storage.ts@@ -6,6 +6,9 @@ interface StorageData {name: string;stepIndex: number;};+ hint: {+ name: string;+ };bytebase_options: {appearance: {language: string;diff --git a/frontend/src/plugins/demo/style.css b/frontend/src/plugins/demo/style.cssindex ac035de65b..99f6dbba7a 100644--- a/frontend/src/plugins/demo/style.css+++ b/frontend/src/plugins/demo/style.css@@ -18,22 +18,26 @@rgb(33 33 33 / 50%) 0px 0px 0px 5000px;}-.bb-guide-dialog {+.bb-guide-dialog,+.bb-hint-dialog {@apply absolute bg-white w-72 m-1 text-black p-2 px-3 rounded-lg text-left -top-full -left-full;box-shadow: 0 3px 30px rgb(33 33 33 / 30%);z-index: 1000000;}-.bb-guide-close-button {+.bb-guide-close-button,+.bb-hint-close-button {@apply absolute top-0 right-0.5 px-1 font-thin rounded cursor-pointer opacity-60 hover:opacity-50;font-size: 24px;}-.bb-guide-title-text {+.bb-guide-title-text,+.bb-hint-title-text {@apply text-lg text-black leading-6 py-1 my-1 pr-3 break-words;}-.bb-guide-description-text {+.bb-guide-description-text,+.bb-hint-description-text {@apply text-base text-gray-700 break-words;}@@ -44,3 +48,24 @@.bb-guide-btns-container > button {@apply px-2 py-1 border text-sm rounded hover:opacity-80 hover:border-gray-400;}++.bb-hint-highlight-wrapper {+ @apply absolute rounded transition-all cursor-pointer;+ z-index: 999999;+ box-shadow: rgb(78 70 211 / 80%) 0px 0px 1px 2px;+ animation: 0.5s linear 0.5s infinite alternate breathing;+}++@keyframes breathing {+ 0% {+ box-shadow: rgb(78 70 211) 0px 0px 1px 2px;+ }+ 30% {+ box-shadow: rgb(78 70 211) 0px 0px 1px 2px,+ rgb(78 70 211 / 40%) 0px 0px 0px 4px;+ }+ 100% {+ box-shadow: rgb(78 70 211) 0px 0px 1px 2px,+ rgb(78 70 211 / 40%) 0px 0px 0px 12px;+ }+}diff --git a/frontend/src/plugins/demo/types.ts b/frontend/src/plugins/demo/types.tsindex 7da05982f3..e52f8db180 100644--- a/frontend/src/plugins/demo/types.ts+++ b/frontend/src/plugins/demo/types.ts@@ -1,6 +1,6 @@type StepType = "click" | "change";-export type GuidePosition = "bottom" | "top" | "left" | "right";+export type DialogPosition = "bottom" | "top" | "left" | "right";export type I18NText = {[key: string]: string;@@ -16,7 +16,7 @@ export interface StepData {// value is the regex-like string using for check the target content valuevalue?: string;// position is the position of the guide dialog (default is bottom)- position?: GuidePosition;+ position?: DialogPosition;// cover is the flag that cover should be showncover?: boolean;// hideNextButton is the flag that next button should be hidden@@ -27,3 +27,18 @@ export interface GuideData {name: string;steps: StepData[];}++// Hint is a special guide that has no Next button and is always shown.+export interface HintData {+ selector: string;+ title: string | I18NText;+ description: string | I18NText;+ // url is using for validate url in change step+ url?: string;+ // position is the position of the guide dialog (default is bottom)+ position?: DialogPosition;+ // cover is the flag that cover should be shown+ cover?: boolean;+ // addtionClass is the class name that should be added to the hint element+ additionStyle?: CSSStyleDeclaration;+}diff --git a/frontend/src/plugins/demo/utils.ts b/frontend/src/plugins/demo/utils.tsindex c0445cb7d5..6ec290858c 100644--- a/frontend/src/plugins/demo/utils.ts+++ b/frontend/src/plugins/demo/utils.ts@@ -89,22 +89,26 @@ export const getElementMaxZIndex = (element: HTMLElement): number => {return 0;};-const getTargetElementBySelectors = (selectors: string[][]) => {- let targetElement = document.body;- for (const selector of selectors) {- try {- targetElement = document.body.querySelector(- selector.join(" ")- ) as HTMLElement;- } catch (error) {- // do nth- }+const getTargetElementWithSelector = (selector: string) => {+ let targetElement = null;+ try {+ targetElement = document.body.querySelector(selector) as HTMLElement;+ } catch (error) {+ // do nth+ }+ return targetElement;+};- if (targetElement) {- break;+const getTargetElementBySelectors = (selectorsList: string[][]) => {+ for (const selectors of selectorsList) {+ for (const selector of selectors) {+ const targetElement = getTargetElementWithSelector(selector);+ if (targetElement) {+ return targetElement;+ }}}- return targetElement;+ return null;};// waitForTargetElement will wait for the target element to be available in the DOM.diff --git a/frontend/static/demo/schema-migration/hint.json b/frontend/static/demo/schema-migration/hint.jsonnew file mode 100644index 0000000000..44ef16e3a9--- /dev/null+++ b/frontend/static/demo/schema-migration/hint.json@@ -0,0 +1,14 @@+[+ {+ "selector": "[data-label='bb-alter-schema-button']",+ "title": "",+ "description": {+ "en-US": "Alter schema is a main feature of Bytebase",+ "zh-CN": "修改数据库 Schema 是 Bytebase 的核心功能之一"+ },+ "url": "http://localhost:3000",+ "additionStyle": {+ "z-index": "10"+ }+ }+]