lwc-plugin-rectangles
Version:
310 lines (309 loc) • 8.21 kB
JavaScript
var x = Object.defineProperty;
var w = (s, t, e) => t in s ? x(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
var r = (s, t, e) => (w(s, typeof t != "symbol" ? t + "" : t, e), e);
import { isBusinessDay as f } from "lightweight-charts";
function c(s, t, e) {
const i = Math.round(e * s), o = Math.round(e * t);
return {
position: Math.min(i, o),
length: Math.abs(o - i) + 1
};
}
class m {
constructor(t, e, i, o) {
r(this, "_p1");
r(this, "_p2");
r(this, "_fillColor");
r(this, "_vertical", !1);
this._p1 = t, this._p2 = e, this._fillColor = i, this._vertical = o;
}
draw(t) {
t.useBitmapCoordinateSpace((e) => {
if (this._p1 === null || this._p2 === null)
return;
const i = e.context;
i.globalAlpha = 0.5;
const o = c(
this._p1,
this._p2,
this._vertical ? e.verticalPixelRatio : e.horizontalPixelRatio
);
i.fillStyle = this._fillColor, this._vertical ? i.fillRect(0, o.position, 15, o.length) : i.fillRect(o.position, 0, o.length, 15);
});
}
}
class u {
constructor(t, e) {
r(this, "_source");
r(this, "_p1", null);
r(this, "_p2", null);
r(this, "_vertical", !1);
this._source = t, this._vertical = e;
}
update() {
[this._p1, this._p2] = this.getPoints();
}
renderer() {
return new m(
this._p1,
this._p2,
this._source.options.fillColor,
this._vertical
);
}
zOrder() {
return "bottom";
}
}
class g extends u {
getPoints() {
const t = this._source.series, e = t.priceToCoordinate(this._source.p1.price), i = t.priceToCoordinate(this._source.p2.price);
return [e, i];
}
}
class V extends u {
getPoints() {
const t = this._source.chart.timeScale(), e = t.timeToCoordinate(this._source.p1.time), i = t.timeToCoordinate(this._source.p2.time);
return [e, i];
}
}
class d {
constructor(t, e) {
r(this, "_source");
r(this, "_p");
r(this, "_pos", null);
this._source = t, this._p = e;
}
coordinate() {
return this._pos ?? -1;
}
visible() {
return this._source.options.showLabels;
}
tickVisible() {
return this._source.options.showLabels;
}
textColor() {
return this._source.options.labelTextColor;
}
backColor() {
return this._source.options.labelColor;
}
movePoint(t) {
this._p = t, this.update();
}
}
class h extends d {
update() {
const t = this._source.chart.timeScale();
this._pos = t.timeToCoordinate(this._p.time);
}
text() {
return this._source.options.timeLabelFormatter(this._p.time);
}
}
class p extends d {
update() {
const t = this._source.series;
this._pos = t.priceToCoordinate(this._p.price);
}
text() {
return this._source.options.priceLabelFormatter(this._p.price);
}
}
const C = {
//* Define the default values for all the primitive options.
fillColor: "rgba(200, 50, 100, 0.75)",
labelColor: "rgba(200, 50, 100, 1)",
labelTextColor: "white",
showLabels: !0,
borderType: "none",
priceLabelFormatter: (s) => s.toFixed(2),
timeLabelFormatter: (s) => typeof s == "string" ? s : (f(s) ? new Date(s.year, s.month, s.day) : new Date(s * 1e3)).toLocaleDateString()
};
class y {
constructor(t, e, i, o) {
r(this, "_p1");
r(this, "_p2");
r(this, "_fillColor");
r(this, "_borderType");
this._p1 = t, this._p2 = e, this._fillColor = i, this._borderType = o;
}
draw(t) {
t.useBitmapCoordinateSpace((e) => {
if (this._p1.x === null || this._p1.y === null || this._p2.x === null || this._p2.y === null)
return;
const i = e.context, o = c(
this._p1.x,
this._p2.x,
e.horizontalPixelRatio
), n = c(
this._p1.y,
this._p2.y,
e.verticalPixelRatio
);
i.fillStyle = this._fillColor, this._borderType === "dashed" ? (i.strokeStyle = "white", i.lineWidth = 2, i.setLineDash([2, 4]), i.strokeRect(
o.position,
n.position,
o.length,
n.length
)) : i.fillRect(
o.position,
n.position,
o.length,
n.length
);
});
}
}
class b {
constructor(t) {
r(this, "_source");
r(this, "_p1", { x: null, y: null });
r(this, "_p2", { x: null, y: null });
this._source = t;
}
update() {
if (!this._source.series || !this._source.chart) {
console.warn("⏳ Skipping update: Series or Chart not ready!");
return;
}
const t = this._source.series, e = this._source.chart.timeScale();
if (!e) {
console.warn("⏳ Skipping update: Time scale not ready!");
return;
}
const i = e.timeToCoordinate(this._source.p1.time) ?? this._p1.x, o = e.timeToCoordinate(this._source.p2.time) ?? this._p2.x, n = t.priceToCoordinate(this._source.p1.price) ?? this._p1.y, a = t.priceToCoordinate(this._source.p2.price) ?? this._p2.y;
(i === null || o === null) && console.warn(
"⚠️ timeToCoordinate returned null! p1:",
this._source.p1,
"p2:",
this._source.p2
), (n === null || a === null) && console.warn(
"⚠️ priceToCoordinate returned null! p1:",
this._source.p1,
"p2:",
this._source.p2
), this._p1 = { x: i, y: n }, this._p2 = { x: o, y: a };
}
renderer() {
return new y(
this._p1,
this._p2,
this._source.options.fillColor,
this._source.options.borderType
);
}
}
function _(s) {
if (s === void 0)
throw new Error("Value is undefined");
return s;
}
class P {
constructor() {
r(this, "_chart");
r(this, "_series");
r(this, "_requestUpdate");
}
requestUpdate() {
this._requestUpdate && this._requestUpdate();
}
attached({
chart: t,
series: e,
requestUpdate: i
}) {
this._chart = t, this._series = e, this._series.subscribeDataChanged(this._fireDataUpdated), this._requestUpdate = i, this.requestUpdate();
}
detached() {
this._chart = void 0, this._series = void 0, this._requestUpdate = void 0;
}
get chart() {
return _(this._chart);
}
get series() {
return _(this._series);
}
_fireDataUpdated(t) {
this.dataUpdated && this.dataUpdated(t);
}
}
class R extends P {
constructor(e, i, o = {}) {
super();
r(this, "_options");
r(this, "_p1");
r(this, "_p2");
r(this, "_paneViews");
r(this, "_timeAxisViews");
r(this, "_priceAxisViews");
r(this, "_priceAxisPaneViews");
r(this, "_timeAxisPaneViews");
this._p1 = e, this._p2 = i;
const n = {
...C,
showLabels: !1
};
this._options = {
...n,
...o
}, this._paneViews = [new b(this)], this._timeAxisViews = [
new h(this, e),
new h(this, i)
], this._priceAxisViews = [
new p(this, e),
new p(this, i)
], this._priceAxisPaneViews = [new g(this, !0)], this._timeAxisPaneViews = [new V(this, !1)];
}
updateAllViews() {
this._paneViews.forEach((e) => e.update()), this._timeAxisViews.forEach((e) => e.update()), this._priceAxisViews.forEach((e) => e.update()), this._priceAxisPaneViews.forEach((e) => e.update()), this._timeAxisPaneViews.forEach((e) => e.update());
}
priceAxisViews() {
return [];
}
timeAxisViews() {
return [];
}
paneViews() {
return this._paneViews;
}
priceAxisPaneViews() {
return [];
}
timeAxisPaneViews() {
return [];
}
autoscaleInfo(e, i) {
return this._timeCurrentlyVisible(this.p1.time, e, i) || this._timeCurrentlyVisible(this.p2.time, e, i) ? {
priceRange: {
minValue: Math.min(this.p1.price, this.p2.price),
maxValue: Math.max(this.p1.price, this.p2.price)
}
} : null;
}
dataUpdated() {
}
_timeCurrentlyVisible(e, i, o) {
const n = this.chart.timeScale(), a = n.timeToCoordinate(e);
if (a === null)
return !1;
const l = n.coordinateToLogical(a);
return l === null ? !1 : l <= o && l >= i;
}
get options() {
return this._options;
}
applyOptions(e) {
this._options = { ...this._options, ...e }, this.requestUpdate();
}
get p1() {
return this._p1;
}
get p2() {
return this._p2;
}
}
export {
R as Rectangles
};