inline-style-editor 1.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/.vscode/launch.json +14 -0
- package/README.md +45 -0
- package/dist/inline-style-editor.css +1 -0
- package/dist/inline-style-editor.js +8 -0
- package/dist/inline-style-editor.mjs +5626 -0
- package/docs/index.html +353 -0
- package/docs/inline-style-editor.css +1 -0
- package/docs/inline-style-editor.js +8 -0
- package/package.json +38 -0
- package/rollup.config.js +40 -0
- package/src/assets/index.scss +2 -0
- package/src/assets/style.scss +144 -0
- package/src/components/ColorPicker.svelte +52 -0
- package/src/components/StyleEditor.svelte +379 -0
- package/src/index.js +7 -0
- package/src/util/boxesContour.js +82 -0
- package/src/util/fonts.js +20 -0
- package/src/util/path.js +47 -0
- package/stats.html +4034 -0
- package/stats.json +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { contours } from 'd3-contour';
|
|
2
|
+
import { geoPath, geoIdentity } from 'd3-geo';
|
|
3
|
+
import { simplifyLinesPath } from './path';
|
|
4
|
+
|
|
5
|
+
function pointInBox(p, box) {
|
|
6
|
+
return !(p.x < box.left || p.x > box.right || p.y > box.bottom || p.y < box.top)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function pointInBoxes(point, boxes) {
|
|
10
|
+
for (const box of boxes){
|
|
11
|
+
if (pointInBox(point, box)) return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function defineRectangleClockWise({width, height, top = 0, left = 0}) {
|
|
17
|
+
return `M${left} ${top} h${width} v${height} h-${width}z`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function defineRectangleAntiClockWise({width, height, top = 0, left = 0}) {
|
|
21
|
+
return `M${left} ${top} v${height} h${width} v-${height}z`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function boxOverlap(box1, box2) {
|
|
25
|
+
return (box1.right >= box2.left && box2.right >= box1.left)
|
|
26
|
+
&& (box1.bottom >= box2.top && box2.bottom >= box1.top)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function boxesHaveOverlap(boundingBoxes) {
|
|
30
|
+
for (let i = 0; i < boundingBoxes.length - 1; ++i) {
|
|
31
|
+
const ref = boundingBoxes[i];
|
|
32
|
+
for (let j = i + 1; j < boundingBoxes.length; ++j) {
|
|
33
|
+
if (boxOverlap(ref, boundingBoxes[j])) return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function computeContours(boundingBoxes, pageDimensions) {
|
|
40
|
+
let _pathWithHoles = defineRectangleAntiClockWise({width: pageDimensions.width, height: pageDimensions.height});
|
|
41
|
+
if (boundingBoxes.length < 10 && !boxesHaveOverlap(boundingBoxes)) {
|
|
42
|
+
for (const bbox of boundingBoxes) {
|
|
43
|
+
_pathWithHoles = `${_pathWithHoles} ${defineRectangleClockWise(bbox)}`;
|
|
44
|
+
}
|
|
45
|
+
return _pathWithHoles;
|
|
46
|
+
}
|
|
47
|
+
const minX = Math.min(...boundingBoxes.map(rect => rect.left));
|
|
48
|
+
const minY = Math.min(...boundingBoxes.map(rect => rect.top));
|
|
49
|
+
const offsetBoxes = boundingBoxes.map(rect => {
|
|
50
|
+
rect.left = rect.left - minX;
|
|
51
|
+
rect.right = rect.right - minX;
|
|
52
|
+
rect.top = rect.top - minY;
|
|
53
|
+
rect.bottom = rect.bottom - minY;
|
|
54
|
+
return rect;
|
|
55
|
+
});
|
|
56
|
+
offsetBoxes.sort((a, b) => {
|
|
57
|
+
if (a.left > b.left) return 1;
|
|
58
|
+
if (a.left < b.left) return -1;
|
|
59
|
+
return 0;
|
|
60
|
+
});
|
|
61
|
+
const maxX = Math.ceil(Math.max(...offsetBoxes.map(rect => rect.right)));
|
|
62
|
+
const maxY = Math.ceil(Math.max(...offsetBoxes.map(rect => rect.bottom)));
|
|
63
|
+
const maxNbPixels = 20000;
|
|
64
|
+
const downscaleFactor = (maxX * maxY) / maxNbPixels;
|
|
65
|
+
const resX = Math.ceil(maxX / downscaleFactor);
|
|
66
|
+
const resY = Math.ceil(maxY / downscaleFactor);
|
|
67
|
+
// console.log(resX, resY);
|
|
68
|
+
const values = new Array(resX * resY); // one coordinate per pixel
|
|
69
|
+
for (let j = 0, k = 0; j < resY; ++j) {
|
|
70
|
+
for (let i = 0; i < resX; ++i, ++k) {
|
|
71
|
+
values[k] = pointInBoxes({x: i * downscaleFactor, y: j * downscaleFactor}, offsetBoxes) ? 1 : 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const computedContours = contours().size([resX, resY]).thresholds([1])(values);
|
|
75
|
+
const projection = geoIdentity().fitExtent([[minX, minY], [minX + maxX, minY + maxY]], computedContours[0]);
|
|
76
|
+
const path = geoPath().projection(projection);
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
return `${_pathWithHoles} ${simplifyLinesPath(path(computedContours[0]))}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { computeContours };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const fontCheck = new Set([
|
|
2
|
+
// Windows 10
|
|
3
|
+
'Arial', 'Arial Black', 'Bahnschrift', 'Calibri', 'Cambria', 'Cambria Math', 'Candara', 'Comic Sans MS', 'Consolas', 'Constantia', 'Corbel', 'Courier New', 'Ebrima', 'Franklin Gothic Medium', 'Gabriola', 'Gadugi', 'Georgia', 'HoloLens MDL2 Assets', 'Impact', 'Ink Free', 'Javanese Text', 'Leelawadee UI', 'Lucida Console', 'Lucida Sans Unicode', 'Malgun Gothic', 'Marlett', 'Microsoft Himalaya', 'Microsoft JhengHei', 'Microsoft New Tai Lue', 'Microsoft PhagsPa', 'Microsoft Sans Serif', 'Microsoft Tai Le', 'Microsoft YaHei', 'Microsoft Yi Baiti', 'MingLiU-ExtB', 'Mongolian Baiti', 'MS Gothic', 'MV Boli', 'Myanmar Text', 'Nirmala UI', 'Palatino Linotype', 'Segoe MDL2 Assets', 'Segoe Print', 'Segoe Script', 'Segoe UI', 'Segoe UI Historic', 'Segoe UI Emoji', 'Segoe UI Symbol', 'SimSun', 'Sitka', 'Sylfaen', 'Symbol', 'Tahoma', 'Times New Roman', 'Trebuchet MS', 'Verdana', 'Webdings', 'Wingdings', 'Yu Gothic',
|
|
4
|
+
// macOS
|
|
5
|
+
'American Typewriter', 'Andale Mono', 'Arial', 'Arial Black', 'Arial Narrow', 'Arial Rounded MT Bold', 'Arial Unicode MS', 'Avenir', 'Avenir Next', 'Avenir Next Condensed', 'Baskerville', 'Big Caslon', 'Bodoni 72', 'Bodoni 72 Oldstyle', 'Bodoni 72 Smallcaps', 'Bradley Hand', 'Brush Script MT', 'Chalkboard', 'Chalkboard SE', 'Chalkduster', 'Charter', 'Cochin', 'Comic Sans MS', 'Copperplate', 'Courier', 'Courier New', 'Didot', 'DIN Alternate', 'DIN Condensed', 'Futura', 'Geneva', 'Georgia', 'Gill Sans', 'Helvetica', 'Helvetica Neue', 'Herculanum', 'Hoefler Text', 'Impact', 'Lucida Grande', 'Luminari', 'Marker Felt', 'Menlo', 'Microsoft Sans Serif', 'Monaco', 'Noteworthy', 'Optima', 'Palatino', 'Papyrus', 'Phosphate', 'Rockwell', 'Savoye LET', 'SignPainter', 'Skia', 'Snell Roundhand', 'Tahoma', 'Times', 'Times New Roman', 'Trattatello', 'Trebuchet MS', 'Verdana', 'Zapfino',
|
|
6
|
+
].sort());
|
|
7
|
+
|
|
8
|
+
function getFonts() {
|
|
9
|
+
const availableFonts = new Set();
|
|
10
|
+
|
|
11
|
+
for (const font of fontCheck.values()) {
|
|
12
|
+
if (document.fonts.check(`12px "${font}"`)) {
|
|
13
|
+
availableFonts.add(font);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return [...availableFonts.values()]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { getFonts };
|
package/src/util/path.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
import parsePath from 'parse-svg-path';
|
|
3
|
+
|
|
4
|
+
export function simplifyLinesPath(path) {
|
|
5
|
+
const parsed = parsePath(path).map(values => values.map((v, i) => {
|
|
6
|
+
if (i > 0) return Math.round(v);
|
|
7
|
+
return v;
|
|
8
|
+
}));
|
|
9
|
+
const simplifiedPath = [];
|
|
10
|
+
for (let i = 0; i < parsed.length - 1; ++i) {
|
|
11
|
+
const current = parsed[i];
|
|
12
|
+
let next = parsed[i + 1];
|
|
13
|
+
simplifiedPath.push(current);
|
|
14
|
+
let sameX = current[1] === next[1];
|
|
15
|
+
let sameY = current[2] === next[2];
|
|
16
|
+
if (sameX) {
|
|
17
|
+
while(sameX) {
|
|
18
|
+
i += 1;
|
|
19
|
+
next = parsed[i + 1];
|
|
20
|
+
sameX = next[0] === "L" && current[1] === next[1];
|
|
21
|
+
}
|
|
22
|
+
i -= 1;
|
|
23
|
+
}
|
|
24
|
+
if (sameY) {
|
|
25
|
+
while(sameY) {
|
|
26
|
+
i += 1;
|
|
27
|
+
next = parsed[i + 1];
|
|
28
|
+
sameY = next[0] === "L" && current[2] === next[2];
|
|
29
|
+
}
|
|
30
|
+
i -= 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
simplifiedPath.push(parsed[parsed.length - 1]);
|
|
34
|
+
return instructionsToPath(simplifiedPath);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function instructionsToPath(data) {
|
|
38
|
+
let str = '';
|
|
39
|
+
data.forEach(ins => {
|
|
40
|
+
for (let i = 0; i < ins.length; ++i) {
|
|
41
|
+
let val = ins[i];
|
|
42
|
+
if (i > 0) val = Math.round(val);
|
|
43
|
+
str += val + (i === ins.length - 1 ? '' : ' ');
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return str;
|
|
47
|
+
}
|