textchecker-element
Version:
Overlay text checker web compoentns.
216 lines • 10.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextCheckerElement = void 0;
const text_caret_pos_1 = __importDefault(require("text-caret-pos"));
const lit_html_1 = require("lit-html");
const textchecker_store_1 = require("./textchecker-store");
const toPX = require("to-px");
const Marker = (rect, isHighLight = false) => {
if (isHighLight) {
return lit_html_1.html `<span
style="pointer-events: none; border: 2px dotted red; position: absolute; left: ${rect.left}px; top: ${rect.top}px; width: ${rect.width}px; height: ${rect.height}px;"
></span>`;
}
else {
return lit_html_1.html `<span
style="pointer-events: none; border-bottom: 2px dotted red; position: absolute; left: ${rect.left}px; top: ${rect.top}px; width: ${rect.width}px; height: ${rect.height}px;"
></span>`;
}
};
class TextCheckerElement extends HTMLElement {
constructor(args) {
super();
this.renderAnnotationMarkers = (state) => {
const items = state.rectItems.map((rect) => Marker(rect, state.highlightRectIdSet.has(rect.index)));
lit_html_1.render(items, this.annotationBox);
};
this.onMouseUpdate = (event) => {
const state = this.store.get();
const hoverPadding = this.hoverPadding;
const isIncludedIndexes = state.rectItems
.filter((rect) => {
const point = {
x: event.clientX - rect.boxAbsoluteX,
y: event.clientY - rect.boxAbsoluteY
};
return (rect.left - hoverPadding <= point.x &&
point.x <= rect.left + hoverPadding + rect.width &&
rect.top - hoverPadding <= point.y &&
point.y <= rect.top + rect.height + hoverPadding);
})
.map((item) => item.index);
// call mouseover
state.rectItems.forEach((item) => {
var _a, _b;
const currentState = state.mouseHoverReactIdMap.get(item.index);
const isIncludedMouse = isIncludedIndexes.includes(item.index);
if (currentState === false && isIncludedMouse) {
(_a = state.annotationItems[item.index]) === null || _a === void 0 ? void 0 : _a.onMouseEnter({
rectItem: item
});
}
else if (currentState === true && !isIncludedMouse) {
(_b = state.annotationItems[item.index]) === null || _b === void 0 ? void 0 : _b.onMouseLeave({
rectItem: item
});
}
state.mouseHoverReactIdMap.set(item.index, isIncludedMouse);
});
// update highlight
this.store.highlightRectIndexes(isIncludedIndexes);
};
this.targetElement = args.targetElement;
this.hoverPadding = args.hoverPadding;
this.store = textchecker_store_1.createTextCheckerStore();
}
connectedCallback() {
const target = this.targetElement;
if (!target) {
throw new Error("target element is not found");
}
const shadow = this.attachShadow({ mode: "open" });
const overlay = document.createElement("div");
overlay.setAttribute("style", "color: transparent; border: 1px dotted blue; position: absolute; top: 0px; left: 0px; pointer-events: none;");
const annotationBox = document.createElement("div");
overlay.append(annotationBox);
shadow.append(overlay);
this.annotationBox = annotationBox;
this.targetElement.addEventListener("mousemove", this.onMouseUpdate);
this.store.onChange(() => {
this.renderAnnotationMarkers(this.store.get());
});
}
updateAnnotations(annotationItems) {
var _a, _b, _c, _d, _e, _f;
const target = this.targetElement;
const targetStyle = window.getComputedStyle(target);
const copyAttributes = [
"box-sizing",
"overflow",
];
const copyStyle = copyAttributes
.map((attr) => {
return `${attr}: ${targetStyle.getPropertyValue(attr)};`;
})
.join("");
this.annotationBox.setAttribute("style", `color: transparent; position: absolute; pointer-events: none; ${copyStyle}`);
// Ref: https://github.com/yuku/textoverlay
// update annotation box that align with target textarea
// top-left (0,0)
// read styles form target element
const top = target.offsetTop;
const left = target.offsetLeft;
const height = target.offsetHeight;
const width = target.clientWidth +
parseInt(targetStyle.borderLeftWidth || '0', 10) +
parseInt(targetStyle.borderRightWidth || '0', 10);
// const textareaScrollTop = target.scrollTop;
const textareaZIndex = targetStyle.zIndex !== null &&
targetStyle.zIndex !== 'auto'
? +targetStyle.zIndex :
0;
// updates style
this.annotationBox.style.zIndex = `${textareaZIndex + 1}`;
this.annotationBox.style.left = `${left}px`;
this.annotationBox.style.top = `${top}px`;
this.annotationBox.style.height = `${height}px`;
this.annotationBox.style.width = `${width}px`;
// box
const fontSize = (_a = toPX(targetStyle.getPropertyValue("font-size"))) !== null && _a !== void 0 ? _a : 16.123;
const boxMarginTop = (_b = toPX(targetStyle.getPropertyValue("margin-top"))) !== null && _b !== void 0 ? _b : 0;
const boxMarginBottom = (_c = toPX(targetStyle.getPropertyValue("margin-bottom"))) !== null && _c !== void 0 ? _c : 0;
const boxBorderWidth = (_d = toPX(targetStyle.getPropertyValue("border-width"))) !== null && _d !== void 0 ? _d : 0;
const boxPaddingTop = (_e = toPX(targetStyle.getPropertyValue("padding-top"))) !== null && _e !== void 0 ? _e : 0;
const boxPaddingBottom = (_f = toPX(targetStyle.getPropertyValue("padding-bottom"))) !== null && _f !== void 0 ? _f : 0;
const boundingClientRect = target.getBoundingClientRect();
const boxAbsoluteX = boundingClientRect.x;
const boxAbsoluteY = boundingClientRect.y;
const boxWidth = boundingClientRect.width;
const boxHeight = boundingClientRect.height;
const rectItems = annotationItems.flatMap((annotation, index) => {
var _a, _b, _c, _d, _e, _f;
const start = annotation.start;
const end = annotation.end;
// 0 start
const startCoordinate = text_caret_pos_1.default.getCoordinates(this.targetElement, start, {
reuse: true,
returnHeight: true,
returnDiv: true,
debug: false
});
const endCoordinate = text_caret_pos_1.default.getCoordinates(this.targetElement, end, {
reuse: true,
returnHeight: true,
returnDiv: true,
debug: false
});
const rectItems = startCoordinate.top === endCoordinate.top
? [
{
index,
left: startCoordinate.left,
top: startCoordinate.top,
height: fontSize,
width: endCoordinate.left - startCoordinate.left,
boxMarginTop,
boxMarginBottom,
boxBorderWidth,
boxAbsoluteX,
boxAbsoluteY,
boxWidth,
boxHeight,
boxPaddingTop,
boxPaddingBottom
}
]
: // two line
[
{
index,
left: startCoordinate.left,
top: startCoordinate.top,
height: fontSize,
width: ((_c = (_b = (_a = startCoordinate === null || startCoordinate === void 0 ? void 0 : startCoordinate._div) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.width) !== null && _c !== void 0 ? _c : 0) - startCoordinate.left,
boxMarginTop,
boxMarginBottom,
boxBorderWidth,
boxAbsoluteX,
boxAbsoluteY,
boxWidth,
boxHeight,
boxPaddingTop,
boxPaddingBottom
},
{
index,
left: 0,
top: endCoordinate.top,
height: fontSize,
width: ((_f = (_e = (_d = startCoordinate === null || startCoordinate === void 0 ? void 0 : startCoordinate._div) === null || _d === void 0 ? void 0 : _d.getBoundingClientRect()) === null || _e === void 0 ? void 0 : _e.left) !== null && _f !== void 0 ? _f : 0) + endCoordinate.left,
boxMarginTop,
boxMarginBottom,
boxBorderWidth,
boxAbsoluteX,
boxAbsoluteY,
boxWidth,
boxHeight,
boxPaddingTop,
boxPaddingBottom
}
];
return rectItems;
});
this.store.update({
annotationItems,
rectItems
});
}
}
exports.TextCheckerElement = TextCheckerElement;
if (!window.customElements.get("text-checker-element")) {
window.customElements.define("text-checker-element", TextCheckerElement);
}
//# sourceMappingURL=textchecker-element.js.map