@knotx/plugins-background 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +208 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.mts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.mjs +205 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 格桑
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const jsxRuntime = require('@knotx/jsx/jsx-runtime');
|
|
4
|
+
const core = require('@knotx/core');
|
|
5
|
+
const decorators = require('@knotx/decorators');
|
|
6
|
+
|
|
7
|
+
var __create = Object.create;
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __defProps = Object.defineProperties;
|
|
10
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
11
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
12
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
15
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
16
|
+
var __typeError = (msg) => {
|
|
17
|
+
throw TypeError(msg);
|
|
18
|
+
};
|
|
19
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
20
|
+
var __spreadValues = (a, b) => {
|
|
21
|
+
for (var prop in b || (b = {}))
|
|
22
|
+
if (__hasOwnProp.call(b, prop))
|
|
23
|
+
__defNormalProp(a, prop, b[prop]);
|
|
24
|
+
if (__getOwnPropSymbols)
|
|
25
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
26
|
+
if (__propIsEnum.call(b, prop))
|
|
27
|
+
__defNormalProp(a, prop, b[prop]);
|
|
28
|
+
}
|
|
29
|
+
return a;
|
|
30
|
+
};
|
|
31
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
32
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
33
|
+
var __decoratorStart = (base) => {
|
|
34
|
+
var _a2;
|
|
35
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
36
|
+
};
|
|
37
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
38
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
39
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
40
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
41
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
42
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
43
|
+
return value;
|
|
44
|
+
};
|
|
45
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
46
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
47
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
48
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
49
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
50
|
+
return __privateGet(this, extra);
|
|
51
|
+
}, set [name](x) {
|
|
52
|
+
return __privateSet(this, extra, x);
|
|
53
|
+
} }, name));
|
|
54
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
55
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
56
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
57
|
+
if (k) {
|
|
58
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
59
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
60
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
61
|
+
}
|
|
62
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
63
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
64
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
65
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
66
|
+
}
|
|
67
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
68
|
+
};
|
|
69
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
70
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
71
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
72
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
73
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
74
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
75
|
+
var _render_dec, _transform_dec, _container_dec, _a, _init;
|
|
76
|
+
var BackgroundVariant = /* @__PURE__ */ ((BackgroundVariant2) => {
|
|
77
|
+
BackgroundVariant2["Lines"] = "lines";
|
|
78
|
+
BackgroundVariant2["Dots"] = "dots";
|
|
79
|
+
BackgroundVariant2["Cross"] = "cross";
|
|
80
|
+
return BackgroundVariant2;
|
|
81
|
+
})(BackgroundVariant || {});
|
|
82
|
+
class Background extends (_a = core.BasePlugin, _container_dec = [decorators.inject.container()], _transform_dec = [decorators.inject.canvas.transform()], _render_dec = [decorators.layer(core.Layer.Background)], _a) {
|
|
83
|
+
constructor(props = {}) {
|
|
84
|
+
super();
|
|
85
|
+
this.props = props;
|
|
86
|
+
__runInitializers(_init, 5, this);
|
|
87
|
+
__publicField(this, "name", "background");
|
|
88
|
+
__publicField(this, "container", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
|
|
89
|
+
__publicField(this, "transform", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
|
|
90
|
+
}
|
|
91
|
+
render() {
|
|
92
|
+
const { container, transform } = core.use(() => ({
|
|
93
|
+
container: this.container,
|
|
94
|
+
transform: this.transform
|
|
95
|
+
}));
|
|
96
|
+
return /* @__PURE__ */ jsxRuntime.jsx(BackgroundComponent, __spreadProps(__spreadValues({}, this.props), { id: this.pluginId, container, transform }));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
_init = __decoratorStart(_a);
|
|
100
|
+
__decorateElement(_init, 1, "render", _render_dec, Background);
|
|
101
|
+
__decorateElement(_init, 5, "container", _container_dec, Background);
|
|
102
|
+
__decorateElement(_init, 5, "transform", _transform_dec, Background);
|
|
103
|
+
__decoratorMetadata(_init, Background);
|
|
104
|
+
const defaultSize = {
|
|
105
|
+
["dots" /* Dots */]: 1,
|
|
106
|
+
["lines" /* Lines */]: 1,
|
|
107
|
+
["cross" /* Cross */]: 6
|
|
108
|
+
};
|
|
109
|
+
function BackgroundComponent({
|
|
110
|
+
id,
|
|
111
|
+
variant = "dots" /* Dots */,
|
|
112
|
+
// only used for dots and cross
|
|
113
|
+
gap = 20,
|
|
114
|
+
// only used for lines and cross
|
|
115
|
+
size,
|
|
116
|
+
lineWidth = 1,
|
|
117
|
+
offset = 0,
|
|
118
|
+
color,
|
|
119
|
+
bgColor,
|
|
120
|
+
style,
|
|
121
|
+
container,
|
|
122
|
+
transform = {
|
|
123
|
+
positionX: 0,
|
|
124
|
+
positionY: 0,
|
|
125
|
+
scale: 1,
|
|
126
|
+
previousScale: 1
|
|
127
|
+
}
|
|
128
|
+
}) {
|
|
129
|
+
const patternSize = size || defaultSize[variant];
|
|
130
|
+
const isDots = variant === "dots" /* Dots */;
|
|
131
|
+
const isCross = variant === "cross" /* Cross */;
|
|
132
|
+
const gapXY = Array.isArray(gap) ? gap : [gap, gap];
|
|
133
|
+
const scaledGap = [gapXY[0] * transform.scale || 1, gapXY[1] * transform.scale || 1];
|
|
134
|
+
const scaledSize = patternSize * transform.scale;
|
|
135
|
+
const offsetXY = Array.isArray(offset) ? offset : [offset, offset];
|
|
136
|
+
const patternDimensions = isCross ? [scaledSize, scaledSize] : scaledGap;
|
|
137
|
+
const scaledOffset = [
|
|
138
|
+
offsetXY[0] * transform.scale || 1 + patternDimensions[0] / 2,
|
|
139
|
+
offsetXY[1] * transform.scale || 1 + patternDimensions[1] / 2
|
|
140
|
+
];
|
|
141
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
142
|
+
"svg",
|
|
143
|
+
{
|
|
144
|
+
style: __spreadValues(__spreadValues({
|
|
145
|
+
"position": "absolute",
|
|
146
|
+
"width": "100%",
|
|
147
|
+
"height": "100%",
|
|
148
|
+
"top": 0,
|
|
149
|
+
"left": 0,
|
|
150
|
+
// @ts-expect-error can use css variable
|
|
151
|
+
"--background-color-props": bgColor,
|
|
152
|
+
"--background-pattern-color-props": color
|
|
153
|
+
}, style), container),
|
|
154
|
+
children: [
|
|
155
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
156
|
+
"pattern",
|
|
157
|
+
{
|
|
158
|
+
id,
|
|
159
|
+
x: transform.positionX % scaledGap[0],
|
|
160
|
+
y: transform.positionY % scaledGap[1],
|
|
161
|
+
width: scaledGap[0],
|
|
162
|
+
height: scaledGap[1],
|
|
163
|
+
patternUnits: "userSpaceOnUse",
|
|
164
|
+
patternTransform: `translate(-${scaledOffset[0]},-${scaledOffset[1]})`,
|
|
165
|
+
children: isDots ? /* @__PURE__ */ jsxRuntime.jsx(DotPattern, { radius: scaledSize / 2 }) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
166
|
+
LinePattern,
|
|
167
|
+
{
|
|
168
|
+
dimensions: patternDimensions,
|
|
169
|
+
lineWidth,
|
|
170
|
+
variant
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
),
|
|
175
|
+
/* @__PURE__ */ jsxRuntime.jsx("rect", { x: "0", y: "0", width: "100%", height: "100%", fill: `url(#${id})` })
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
BackgroundComponent.displayName = "Background";
|
|
181
|
+
function LinePattern({ dimensions, lineWidth, variant }) {
|
|
182
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
183
|
+
"path",
|
|
184
|
+
{
|
|
185
|
+
strokeWidth: lineWidth,
|
|
186
|
+
d: `M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${dimensions[0]}`,
|
|
187
|
+
style: {
|
|
188
|
+
stroke: `var( --background-pattern-color-props, var(--background-pattern-color, var(--background-pattern-${variant}-color-default)) )`
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
function DotPattern({ radius }) {
|
|
194
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
195
|
+
"circle",
|
|
196
|
+
{
|
|
197
|
+
cx: radius,
|
|
198
|
+
cy: radius,
|
|
199
|
+
r: radius,
|
|
200
|
+
style: {
|
|
201
|
+
fill: "var( --background-pattern-color-props, var(--background-pattern-color, var(--background-pattern-dots-color-default)) )"
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
exports.Background = Background;
|
|
208
|
+
exports.BackgroundVariant = BackgroundVariant;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BasePlugin, Container } from '@knotx/core';
|
|
2
|
+
import { CanvasTransformState } from '@knotx/plugins-canvas';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Background plugin
|
|
6
|
+
* Main implementation comes from https://github.com/xyflow/xyflow/blob/main/packages/react/src/additional-components/Background/Background.tsx
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare enum BackgroundVariant {
|
|
10
|
+
Lines = "lines",
|
|
11
|
+
Dots = "dots",
|
|
12
|
+
Cross = "cross"
|
|
13
|
+
}
|
|
14
|
+
declare class Background extends BasePlugin<'background'> {
|
|
15
|
+
private props;
|
|
16
|
+
name: "background";
|
|
17
|
+
constructor(props?: Omit<BackgroundProps, 'id'>);
|
|
18
|
+
container: Container;
|
|
19
|
+
transform: CanvasTransformState;
|
|
20
|
+
render(): JSX.Element;
|
|
21
|
+
}
|
|
22
|
+
interface BackgroundProps {
|
|
23
|
+
id: string;
|
|
24
|
+
/** Color of the pattern */
|
|
25
|
+
color?: string;
|
|
26
|
+
/** Color of the background */
|
|
27
|
+
bgColor?: string;
|
|
28
|
+
/** Gap between repetitions of the pattern */
|
|
29
|
+
gap?: number | [number, number];
|
|
30
|
+
/** Size of a single pattern element */
|
|
31
|
+
size?: number;
|
|
32
|
+
/** Offset of the pattern */
|
|
33
|
+
offset?: number | [number, number];
|
|
34
|
+
/** Line width of the Line pattern */
|
|
35
|
+
lineWidth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Variant of the pattern
|
|
38
|
+
* @example BackgroundVariant.Lines, BackgroundVariant.Dots, BackgroundVariant.Cross
|
|
39
|
+
* 'lines', 'dots', 'cross'
|
|
40
|
+
*/
|
|
41
|
+
variant?: BackgroundVariant;
|
|
42
|
+
/** Style applied to the container */
|
|
43
|
+
style?: Record<string, any>;
|
|
44
|
+
/** Container of the canvas */
|
|
45
|
+
container?: Container;
|
|
46
|
+
/** Transform of the canvas */
|
|
47
|
+
transform?: CanvasTransformState;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { Background, type BackgroundProps, BackgroundVariant };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BasePlugin, Container } from '@knotx/core';
|
|
2
|
+
import { CanvasTransformState } from '@knotx/plugins-canvas';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Background plugin
|
|
6
|
+
* Main implementation comes from https://github.com/xyflow/xyflow/blob/main/packages/react/src/additional-components/Background/Background.tsx
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare enum BackgroundVariant {
|
|
10
|
+
Lines = "lines",
|
|
11
|
+
Dots = "dots",
|
|
12
|
+
Cross = "cross"
|
|
13
|
+
}
|
|
14
|
+
declare class Background extends BasePlugin<'background'> {
|
|
15
|
+
private props;
|
|
16
|
+
name: "background";
|
|
17
|
+
constructor(props?: Omit<BackgroundProps, 'id'>);
|
|
18
|
+
container: Container;
|
|
19
|
+
transform: CanvasTransformState;
|
|
20
|
+
render(): JSX.Element;
|
|
21
|
+
}
|
|
22
|
+
interface BackgroundProps {
|
|
23
|
+
id: string;
|
|
24
|
+
/** Color of the pattern */
|
|
25
|
+
color?: string;
|
|
26
|
+
/** Color of the background */
|
|
27
|
+
bgColor?: string;
|
|
28
|
+
/** Gap between repetitions of the pattern */
|
|
29
|
+
gap?: number | [number, number];
|
|
30
|
+
/** Size of a single pattern element */
|
|
31
|
+
size?: number;
|
|
32
|
+
/** Offset of the pattern */
|
|
33
|
+
offset?: number | [number, number];
|
|
34
|
+
/** Line width of the Line pattern */
|
|
35
|
+
lineWidth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Variant of the pattern
|
|
38
|
+
* @example BackgroundVariant.Lines, BackgroundVariant.Dots, BackgroundVariant.Cross
|
|
39
|
+
* 'lines', 'dots', 'cross'
|
|
40
|
+
*/
|
|
41
|
+
variant?: BackgroundVariant;
|
|
42
|
+
/** Style applied to the container */
|
|
43
|
+
style?: Record<string, any>;
|
|
44
|
+
/** Container of the canvas */
|
|
45
|
+
container?: Container;
|
|
46
|
+
/** Transform of the canvas */
|
|
47
|
+
transform?: CanvasTransformState;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { Background, type BackgroundProps, BackgroundVariant };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BasePlugin, Container } from '@knotx/core';
|
|
2
|
+
import { CanvasTransformState } from '@knotx/plugins-canvas';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Background plugin
|
|
6
|
+
* Main implementation comes from https://github.com/xyflow/xyflow/blob/main/packages/react/src/additional-components/Background/Background.tsx
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare enum BackgroundVariant {
|
|
10
|
+
Lines = "lines",
|
|
11
|
+
Dots = "dots",
|
|
12
|
+
Cross = "cross"
|
|
13
|
+
}
|
|
14
|
+
declare class Background extends BasePlugin<'background'> {
|
|
15
|
+
private props;
|
|
16
|
+
name: "background";
|
|
17
|
+
constructor(props?: Omit<BackgroundProps, 'id'>);
|
|
18
|
+
container: Container;
|
|
19
|
+
transform: CanvasTransformState;
|
|
20
|
+
render(): JSX.Element;
|
|
21
|
+
}
|
|
22
|
+
interface BackgroundProps {
|
|
23
|
+
id: string;
|
|
24
|
+
/** Color of the pattern */
|
|
25
|
+
color?: string;
|
|
26
|
+
/** Color of the background */
|
|
27
|
+
bgColor?: string;
|
|
28
|
+
/** Gap between repetitions of the pattern */
|
|
29
|
+
gap?: number | [number, number];
|
|
30
|
+
/** Size of a single pattern element */
|
|
31
|
+
size?: number;
|
|
32
|
+
/** Offset of the pattern */
|
|
33
|
+
offset?: number | [number, number];
|
|
34
|
+
/** Line width of the Line pattern */
|
|
35
|
+
lineWidth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Variant of the pattern
|
|
38
|
+
* @example BackgroundVariant.Lines, BackgroundVariant.Dots, BackgroundVariant.Cross
|
|
39
|
+
* 'lines', 'dots', 'cross'
|
|
40
|
+
*/
|
|
41
|
+
variant?: BackgroundVariant;
|
|
42
|
+
/** Style applied to the container */
|
|
43
|
+
style?: Record<string, any>;
|
|
44
|
+
/** Container of the canvas */
|
|
45
|
+
container?: Container;
|
|
46
|
+
/** Transform of the canvas */
|
|
47
|
+
transform?: CanvasTransformState;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { Background, type BackgroundProps, BackgroundVariant };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { jsx, jsxs } from '@knotx/jsx/jsx-runtime';
|
|
2
|
+
import { Layer, use, BasePlugin } from '@knotx/core';
|
|
3
|
+
import { inject, layer } from '@knotx/decorators';
|
|
4
|
+
|
|
5
|
+
var __create = Object.create;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defProps = Object.defineProperties;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
10
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
13
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
14
|
+
var __typeError = (msg) => {
|
|
15
|
+
throw TypeError(msg);
|
|
16
|
+
};
|
|
17
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
18
|
+
var __spreadValues = (a, b) => {
|
|
19
|
+
for (var prop in b || (b = {}))
|
|
20
|
+
if (__hasOwnProp.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
if (__getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
24
|
+
if (__propIsEnum.call(b, prop))
|
|
25
|
+
__defNormalProp(a, prop, b[prop]);
|
|
26
|
+
}
|
|
27
|
+
return a;
|
|
28
|
+
};
|
|
29
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
var __decoratorStart = (base) => {
|
|
32
|
+
var _a2;
|
|
33
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
34
|
+
};
|
|
35
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
36
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
37
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
38
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
39
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
40
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
44
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
45
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
46
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
47
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
48
|
+
return __privateGet(this, extra);
|
|
49
|
+
}, set [name](x) {
|
|
50
|
+
return __privateSet(this, extra, x);
|
|
51
|
+
} }, name));
|
|
52
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
53
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
54
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
55
|
+
if (k) {
|
|
56
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
57
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
58
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
59
|
+
}
|
|
60
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
61
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
62
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
63
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
64
|
+
}
|
|
65
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
66
|
+
};
|
|
67
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
68
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
69
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
70
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
71
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
72
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
73
|
+
var _render_dec, _transform_dec, _container_dec, _a, _init;
|
|
74
|
+
var BackgroundVariant = /* @__PURE__ */ ((BackgroundVariant2) => {
|
|
75
|
+
BackgroundVariant2["Lines"] = "lines";
|
|
76
|
+
BackgroundVariant2["Dots"] = "dots";
|
|
77
|
+
BackgroundVariant2["Cross"] = "cross";
|
|
78
|
+
return BackgroundVariant2;
|
|
79
|
+
})(BackgroundVariant || {});
|
|
80
|
+
class Background extends (_a = BasePlugin, _container_dec = [inject.container()], _transform_dec = [inject.canvas.transform()], _render_dec = [layer(Layer.Background)], _a) {
|
|
81
|
+
constructor(props = {}) {
|
|
82
|
+
super();
|
|
83
|
+
this.props = props;
|
|
84
|
+
__runInitializers(_init, 5, this);
|
|
85
|
+
__publicField(this, "name", "background");
|
|
86
|
+
__publicField(this, "container", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
|
|
87
|
+
__publicField(this, "transform", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
|
|
88
|
+
}
|
|
89
|
+
render() {
|
|
90
|
+
const { container, transform } = use(() => ({
|
|
91
|
+
container: this.container,
|
|
92
|
+
transform: this.transform
|
|
93
|
+
}));
|
|
94
|
+
return /* @__PURE__ */ jsx(BackgroundComponent, __spreadProps(__spreadValues({}, this.props), { id: this.pluginId, container, transform }));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
_init = __decoratorStart(_a);
|
|
98
|
+
__decorateElement(_init, 1, "render", _render_dec, Background);
|
|
99
|
+
__decorateElement(_init, 5, "container", _container_dec, Background);
|
|
100
|
+
__decorateElement(_init, 5, "transform", _transform_dec, Background);
|
|
101
|
+
__decoratorMetadata(_init, Background);
|
|
102
|
+
const defaultSize = {
|
|
103
|
+
["dots" /* Dots */]: 1,
|
|
104
|
+
["lines" /* Lines */]: 1,
|
|
105
|
+
["cross" /* Cross */]: 6
|
|
106
|
+
};
|
|
107
|
+
function BackgroundComponent({
|
|
108
|
+
id,
|
|
109
|
+
variant = "dots" /* Dots */,
|
|
110
|
+
// only used for dots and cross
|
|
111
|
+
gap = 20,
|
|
112
|
+
// only used for lines and cross
|
|
113
|
+
size,
|
|
114
|
+
lineWidth = 1,
|
|
115
|
+
offset = 0,
|
|
116
|
+
color,
|
|
117
|
+
bgColor,
|
|
118
|
+
style,
|
|
119
|
+
container,
|
|
120
|
+
transform = {
|
|
121
|
+
positionX: 0,
|
|
122
|
+
positionY: 0,
|
|
123
|
+
scale: 1,
|
|
124
|
+
previousScale: 1
|
|
125
|
+
}
|
|
126
|
+
}) {
|
|
127
|
+
const patternSize = size || defaultSize[variant];
|
|
128
|
+
const isDots = variant === "dots" /* Dots */;
|
|
129
|
+
const isCross = variant === "cross" /* Cross */;
|
|
130
|
+
const gapXY = Array.isArray(gap) ? gap : [gap, gap];
|
|
131
|
+
const scaledGap = [gapXY[0] * transform.scale || 1, gapXY[1] * transform.scale || 1];
|
|
132
|
+
const scaledSize = patternSize * transform.scale;
|
|
133
|
+
const offsetXY = Array.isArray(offset) ? offset : [offset, offset];
|
|
134
|
+
const patternDimensions = isCross ? [scaledSize, scaledSize] : scaledGap;
|
|
135
|
+
const scaledOffset = [
|
|
136
|
+
offsetXY[0] * transform.scale || 1 + patternDimensions[0] / 2,
|
|
137
|
+
offsetXY[1] * transform.scale || 1 + patternDimensions[1] / 2
|
|
138
|
+
];
|
|
139
|
+
return /* @__PURE__ */ jsxs(
|
|
140
|
+
"svg",
|
|
141
|
+
{
|
|
142
|
+
style: __spreadValues(__spreadValues({
|
|
143
|
+
"position": "absolute",
|
|
144
|
+
"width": "100%",
|
|
145
|
+
"height": "100%",
|
|
146
|
+
"top": 0,
|
|
147
|
+
"left": 0,
|
|
148
|
+
// @ts-expect-error can use css variable
|
|
149
|
+
"--background-color-props": bgColor,
|
|
150
|
+
"--background-pattern-color-props": color
|
|
151
|
+
}, style), container),
|
|
152
|
+
children: [
|
|
153
|
+
/* @__PURE__ */ jsx(
|
|
154
|
+
"pattern",
|
|
155
|
+
{
|
|
156
|
+
id,
|
|
157
|
+
x: transform.positionX % scaledGap[0],
|
|
158
|
+
y: transform.positionY % scaledGap[1],
|
|
159
|
+
width: scaledGap[0],
|
|
160
|
+
height: scaledGap[1],
|
|
161
|
+
patternUnits: "userSpaceOnUse",
|
|
162
|
+
patternTransform: `translate(-${scaledOffset[0]},-${scaledOffset[1]})`,
|
|
163
|
+
children: isDots ? /* @__PURE__ */ jsx(DotPattern, { radius: scaledSize / 2 }) : /* @__PURE__ */ jsx(
|
|
164
|
+
LinePattern,
|
|
165
|
+
{
|
|
166
|
+
dimensions: patternDimensions,
|
|
167
|
+
lineWidth,
|
|
168
|
+
variant
|
|
169
|
+
}
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
),
|
|
173
|
+
/* @__PURE__ */ jsx("rect", { x: "0", y: "0", width: "100%", height: "100%", fill: `url(#${id})` })
|
|
174
|
+
]
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
BackgroundComponent.displayName = "Background";
|
|
179
|
+
function LinePattern({ dimensions, lineWidth, variant }) {
|
|
180
|
+
return /* @__PURE__ */ jsx(
|
|
181
|
+
"path",
|
|
182
|
+
{
|
|
183
|
+
strokeWidth: lineWidth,
|
|
184
|
+
d: `M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${dimensions[0]}`,
|
|
185
|
+
style: {
|
|
186
|
+
stroke: `var( --background-pattern-color-props, var(--background-pattern-color, var(--background-pattern-${variant}-color-default)) )`
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
function DotPattern({ radius }) {
|
|
192
|
+
return /* @__PURE__ */ jsx(
|
|
193
|
+
"circle",
|
|
194
|
+
{
|
|
195
|
+
cx: radius,
|
|
196
|
+
cy: radius,
|
|
197
|
+
r: radius,
|
|
198
|
+
style: {
|
|
199
|
+
fill: "var( --background-pattern-color-props, var(--background-pattern-color, var(--background-pattern-dots-color-default)) )"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export { Background, BackgroundVariant };
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@knotx/plugins-background",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "Background Plugin for Knotx",
|
|
6
|
+
"author": "boenfu",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/boenfu/knotx#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/boenfu/knotx.git",
|
|
12
|
+
"directory": "packages/plugins-background"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@knotx/plugins-canvas": "0.0.0",
|
|
33
|
+
"@knotx/core": "0.0.0",
|
|
34
|
+
"@knotx/decorators": "0.0.0",
|
|
35
|
+
"@knotx/jsx": "0.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@knotx/build-config": "0.0.0",
|
|
39
|
+
"@knotx/core": "0.0.0",
|
|
40
|
+
"@knotx/decorators": "0.0.0",
|
|
41
|
+
"@knotx/eslint-config": "0.0.0",
|
|
42
|
+
"@knotx/plugins-canvas": "0.0.0",
|
|
43
|
+
"@knotx/typescript-config": "0.0.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "unbuild --failOnWarn=false",
|
|
47
|
+
"dev": "unbuild --stub",
|
|
48
|
+
"lint": "eslint .",
|
|
49
|
+
"typecheck": "tsc --noEmit"
|
|
50
|
+
}
|
|
51
|
+
}
|