hrw-certificate-editor
Version:
Design Editor Tools with React.js + ant.design + fabric.js
111 lines • 4.49 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debounce_1 = __importDefault(require("lodash/debounce"));
const ReactDOM = __importStar(require("react-dom"));
class TooltipHandler {
constructor(handler) {
/**
* Show tooltip
*
* @param {FabricObject} [target]
*/
this.show = debounce_1.default(async (target) => {
if (target.tooltip && target.tooltip.enabled) {
while (this.tooltipEl.hasChildNodes()) {
this.tooltipEl.removeChild(this.tooltipEl.firstChild);
}
const tooltip = document.createElement('div');
tooltip.className = 'rde-tooltip-right';
let element = target.name;
const { onTooltip } = this.handler;
if (onTooltip) {
element = await onTooltip(tooltip, target);
if (!element) {
return;
}
}
tooltip.innerHTML = element;
this.tooltipEl.appendChild(tooltip);
ReactDOM.render(element, tooltip);
this.tooltipEl.classList.remove('tooltip-hidden');
const zoom = this.handler.canvas.getZoom();
const { clientHeight } = this.tooltipEl;
const { width, height, scaleX, scaleY } = target;
const { left, top } = target.getBoundingRect();
const { _offset: offset } = this.handler.canvas.calcOffset();
const objWidthDiff = width * scaleX * zoom;
const objHeightDiff = (height * scaleY * zoom) / 2 - clientHeight / 2;
const calcLeft = offset.left + left + objWidthDiff;
const calcTop = offset.top + top + objHeightDiff;
if (document.body.clientWidth <= calcLeft + this.tooltipEl.offsetWidth) {
this.tooltipEl.style.left = `${left + offset.left - this.tooltipEl.offsetWidth}px`;
tooltip.className = 'rde-tooltip-left';
}
else {
this.tooltipEl.style.left = `${calcLeft}px`;
}
this.tooltipEl.style.top = `${calcTop}px`;
this.handler.target = target;
}
}, 100);
/**
* Hide tooltip
* @param {fabric.Object} [_target]
*/
this.hide = debounce_1.default((_target) => {
this.handler.target = null;
if (this.tooltipEl) {
this.tooltipEl.classList.add('tooltip-hidden');
}
}, 100);
this.handler = handler;
if (!handler.editable) {
this.initialize();
}
}
/**
* Initialize tooltip
*
* @author salgum1114
*/
initialize() {
this.tooltipEl = document.createElement('div');
this.tooltipEl.id = `${this.handler.id}_tooltip`;
this.tooltipEl.className = 'rde-tooltip tooltip-hidden';
document.body.appendChild(this.tooltipEl);
}
/**
* Destroy tooltip
*
* @author salgum1114
*/
destroy() {
if (this.tooltipEl) {
document.body.removeChild(this.tooltipEl);
}
}
}
exports.default = TooltipHandler;
//# sourceMappingURL=TooltipHandler.js.map