@d3plus/dom 3.0.16 → 3.1.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/es/src/getSize.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { select } from "d3-selection";
2
2
  /**
3
- @desc Given an HTMLElement and a "width" or "height" string, this function returns the current calculated size for the DOM element.
3
+ Given an HTMLElement and a "width" or "height" string, this function returns the current calculated size for the DOM element.
4
4
  @private
5
5
  */ function _elementSize(element, s) {
6
6
  if (!element) return undefined;
@@ -11,32 +11,37 @@ import { select } from "d3-selection";
11
11
  var val = window["inner".concat(s.charAt(0).toUpperCase() + s.slice(1))];
12
12
  var elem = select(element);
13
13
  if (s === "width") {
14
- val -= parseFloat(elem.style("margin-left"), 10);
15
- val -= parseFloat(elem.style("margin-right"), 10);
16
- val -= parseFloat(elem.style("padding-left"), 10);
17
- val -= parseFloat(elem.style("padding-right"), 10);
14
+ val -= parseFloat(elem.style("margin-left"));
15
+ val -= parseFloat(elem.style("margin-right"));
16
+ val -= parseFloat(elem.style("padding-left"));
17
+ val -= parseFloat(elem.style("padding-right"));
18
18
  } else {
19
- val -= parseFloat(elem.style("margin-top"), 10);
20
- val -= parseFloat(elem.style("margin-bottom"), 10);
21
- val -= parseFloat(elem.style("padding-top"), 10);
22
- val -= parseFloat(elem.style("padding-bottom"), 10);
19
+ val -= parseFloat(elem.style("margin-top"));
20
+ val -= parseFloat(elem.style("margin-bottom"));
21
+ val -= parseFloat(elem.style("padding-top"));
22
+ val -= parseFloat(elem.style("padding-bottom"));
23
23
  }
24
24
  return val;
25
25
  } else {
26
- var val1 = parseFloat(select(element).style(s), 10);
26
+ var val1 = element.getBoundingClientRect()[s];
27
27
  if (typeof val1 === "number" && val1 > 0) {
28
28
  if (s === "height") {
29
- val1 -= parseFloat(select(element).style("padding-top"), 10);
30
- val1 -= parseFloat(select(element).style("padding-bottom"), 10);
29
+ val1 -= parseFloat(select(element).style("padding-top"));
30
+ val1 -= parseFloat(select(element).style("padding-bottom"));
31
+ val1 -= parseFloat(select(element).style("border-top"));
32
+ val1 -= parseFloat(select(element).style("border-bottom"));
33
+ } else {
34
+ val1 -= parseFloat(select(element).style("padding-left"));
35
+ val1 -= parseFloat(select(element).style("padding-right"));
36
+ val1 -= parseFloat(select(element).style("border-left"));
37
+ val1 -= parseFloat(select(element).style("border-right"));
31
38
  }
32
39
  return val1;
33
40
  } else return _elementSize(element.parentNode, s);
34
41
  }
35
42
  }
36
43
  /**
37
- @function getSize
38
- @desc Finds the available width and height for a specified HTMLElement, traversing it's parents until it finds something with constrained dimensions. Falls back to the inner dimensions of the browser window if none is found.
39
- @param {HTMLElement} elem The HTMLElement to find dimensions for.
44
+ Finds the available width and height for a specified HTMLElement, traversing it's parents until it finds something with constrained dimensions. Falls back to the inner dimensions of the browser window if none is found.
40
45
  @private
41
46
  */ export default function(elem) {
42
47
  return [
@@ -1,13 +1,11 @@
1
1
  /**
2
- @module inViewport
3
- @desc Returns a *Boolean* denoting whether or not a given DOM element is visible in the current window.
4
- @param {DOMElement} elem The DOM element to analyze.
5
- @param {Number} [buffer = 0] A pixel offset from the edge of the top and bottom of the screen. If a positive value, the element will be deemed visible when it is that many pixels away from entering the viewport. If negative, the element will have to enter the viewport by that many pixels before being deemed visible.
6
- @private
2
+ Determines whether a given DOM element is visible within the current viewport, with an optional pixel buffer.
3
+ @param elem The DOM element to check.
4
+ @param buffer Extra pixel margin around the viewport boundary.
7
5
  */ export default function(elem) {
8
6
  var buffer = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
9
- var pageX = window.pageXOffset !== undefined ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
10
- var pageY = window.pageYOffset !== undefined ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
7
+ var pageX = window.scrollX;
8
+ var pageY = window.scrollY;
11
9
  var bounds = elem.getBoundingClientRect();
12
10
  var height = bounds.height, left = bounds.left + pageX, top = bounds.top + pageY, width = bounds.width;
13
11
  return pageY + window.innerHeight > top + buffer && pageY + buffer < top + height && pageX + window.innerWidth > left + buffer && pageX + buffer < left + width;
@@ -1,8 +1,5 @@
1
- /**
2
- @function isObject
3
- @desc Detects if a variable is a javascript Object.
4
- @param {*} item
5
- */ function _instanceof(left, right) {
1
+ function _instanceof(left, right) {
2
+ "@swc/helpers - instanceof";
6
3
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
7
4
  return !!right[Symbol.hasInstance](left);
8
5
  } else {
@@ -13,6 +10,9 @@ function _type_of(obj) {
13
10
  "@swc/helpers - typeof";
14
11
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
15
12
  }
16
- export default function(item) {
13
+ /**
14
+ Detects if a variable is a javascript Object.
15
+ @param item The value to test.
16
+ */ export default function(item) {
17
17
  return item && (typeof item === "undefined" ? "undefined" : _type_of(item)) === "object" && (typeof window === "undefined" || item !== window && item !== window.document && !_instanceof(item, Element)) && !Array.isArray(item) ? true : false;
18
18
  }
@@ -1,11 +1,10 @@
1
1
  /**
2
- @function parseSides
3
- @desc Converts a string of directional CSS shorthand values into an object with the values expanded.
4
- @param {String|Number} sides The CSS shorthand string to expand.
2
+ Converts a string of directional CSS shorthand values into an object with the values expanded.
3
+ @param sides The CSS shorthand string to expand.
5
4
  */ export default function(sides) {
6
5
  var values;
7
6
  if (typeof sides === "number") values = [
8
- sides
7
+ "".concat(sides)
9
8
  ];
10
9
  else values = sides.split(/\s+/);
11
10
  if (values.length === 1) values = [
package/es/src/rtl.js CHANGED
@@ -1,7 +1,5 @@
1
- import { select } from "d3-selection";
2
1
  /**
3
- @function rtl
4
- @desc Returns `true` if the HTML or body element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl".
2
+ Returns `true` if the HTML or body element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl".
5
3
  */ export default function() {
6
- return select("html").attr("dir") === "rtl" || select("body").attr("dir") === "rtl" || select("html").style("direction") === "rtl" || select("body").style("direction") === "rtl";
7
- };
4
+ return document.documentElement.dir === "rtl" || document.body.dir === "rtl" || getComputedStyle(document.documentElement).direction === "rtl" || getComputedStyle(document.body).direction === "rtl";
5
+ }
package/es/src/stylize.js CHANGED
@@ -1,8 +1,7 @@
1
1
  /**
2
- @function stylize
3
- @desc Applies each key/value in an object as a style.
4
- @param {D3selection} elem The D3 element to apply the styles to.
5
- @param {Object} styles An object of key/value style pairs.
2
+ Applies each key/value in an object as a style.
3
+ @param e The d3 selection to apply styles to.
4
+ @param s An object of key/value style pairs.
6
5
  */ export default function(e) {
7
6
  var s = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
8
7
  for(var k in s)if (({}).hasOwnProperty.call(s, k)) e.style(k, s[k]);
@@ -1,41 +1,46 @@
1
- /**
2
- * Strips HTML and "un-escapes" escape characters.
3
- * @param {String} input
4
- */ function _instanceof(left, right) {
1
+ function _instanceof(left, right) {
2
+ "@swc/helpers - instanceof";
5
3
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
6
4
  return !!right[Symbol.hasInstance](left);
7
5
  } else {
8
6
  return left instanceof right;
9
7
  }
10
8
  }
11
- function htmlDecode(input) {
9
+ import { prepareWithSegments, layoutWithLines } from "@chenglou/pretext";
10
+ /**
11
+ * Strips HTML and "un-escapes" escape characters.
12
+ * @param {String} input
13
+ */ function htmlDecode(input) {
12
14
  if (input.replace(/\s+/g, "") === "") return input;
13
15
  var doc = new DOMParser().parseFromString(input.replace(/<[^>]+>/g, ""), "text/html");
14
16
  return doc.documentElement ? doc.documentElement.textContent : input;
15
17
  }
16
18
  /**
17
- @function textWidth
18
- @desc Given a text string, returns the predicted pixel width of the string when placed into DOM.
19
- @param {String|Array} text Can be either a single string or an array of strings to analyze.
20
- @param {Object} [style] An object of CSS font styles to apply. Accepts any of the valid [CSS font property](http://www.w3schools.com/cssref/pr_font_font.asp) values.
21
- */ export default function(text, style) {
22
- style = Object.assign({
23
- "font-size": 10,
24
- "font-family": "sans-serif",
25
- "font-style": "normal",
26
- "font-weight": 400,
27
- "font-variant": "normal"
28
- }, style);
29
- var context = document.createElement("canvas").getContext("2d");
30
- var font = [];
31
- font.push(style["font-style"]);
32
- font.push(style["font-variant"]);
33
- font.push(style["font-weight"]);
34
- font.push(typeof style["font-size"] === "string" ? style["font-size"] : "".concat(style["font-size"], "px"));
35
- font.push(style["font-family"]);
36
- context.font = font.join(" ");
19
+ * Builds a CSS font shorthand string from a style object.
20
+ * @param {Object} styleObj
21
+ */ function buildFont(styleObj) {
22
+ var style = styleObj["font-style"] || "normal";
23
+ var variant = styleObj["font-variant"] || "normal";
24
+ var weight = styleObj["font-weight"] || 400;
25
+ var size = typeof styleObj["font-size"] === "string" ? styleObj["font-size"] : "".concat(styleObj["font-size"] || 10, "px");
26
+ var family = styleObj["font-family"] || "sans-serif";
27
+ return "".concat(style, " ").concat(variant, " ").concat(weight, " ").concat(size, " ").concat(family);
28
+ }
29
+ /**
30
+ * Measures the width of a single text string using pretext.
31
+ * @param {String} text
32
+ * @param {String} font CSS font shorthand
33
+ */ function measureWidth(text, font) {
34
+ if (!text) return 0;
35
+ var prepared = prepareWithSegments(text, font);
36
+ var result = layoutWithLines(prepared, Infinity, 20);
37
+ return result.lines.length ? result.lines[0].width : 0;
38
+ }
39
+ export default function(text) {
40
+ var style = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
41
+ var font = buildFont(style);
37
42
  if (_instanceof(text, Array)) return text.map(function(t) {
38
- return context.measureText(htmlDecode(t)).width;
43
+ return measureWidth(htmlDecode(t), font);
39
44
  });
40
- return context.measureText(htmlDecode(text)).width;
45
+ return measureWidth(htmlDecode(text), font);
41
46
  }
package/package.json CHANGED
@@ -1,18 +1,25 @@
1
1
  {
2
2
  "name": "@d3plus/dom",
3
- "version": "3.0.16",
3
+ "version": "3.1.0",
4
4
  "description": "JavaScript functions for manipulating and analyzing DOM elements.",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "exports": "./es/index.js",
7
+ "types": "./types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "default": "./es/index.js"
12
+ }
13
+ },
8
14
  "browser": "./umd/d3plus-dom.full.js",
9
15
  "engines": {
10
- "node": ">=18"
16
+ "node": ">=20"
11
17
  },
12
18
  "sideEffects": false,
13
19
  "files": [
14
20
  "umd",
15
- "es"
21
+ "es",
22
+ "types"
16
23
  ],
17
24
  "homepage": "https://d3plus.org",
18
25
  "repository": {
@@ -27,14 +34,16 @@
27
34
  "data",
28
35
  "visualization"
29
36
  ],
37
+ "dependencies": {
38
+ "@chenglou/pretext": "^0.0.3",
39
+ "d3-selection": "^3.0.0",
40
+ "d3-transition": "^3.0.1"
41
+ },
30
42
  "scripts": {
31
43
  "build:esm": "node ../../scripts/build-esm.js",
44
+ "build:types": "tsc",
32
45
  "build:umd": "node ../../scripts/build-umd.js",
33
46
  "dev": "node ../../scripts/dev.js",
34
- "test": "eslint index.js src/**/*.js && eslint --global=it test && mocha 'test/**/*-test.js'"
35
- },
36
- "dependencies": {
37
- "d3-selection": "^3.0.0",
38
- "d3-transition": "^3.0.1"
47
+ "test": "eslint index.ts src/**/*.ts && eslint --global=it test && mocha 'test/**/*-test.js'"
39
48
  }
40
49
  }
@@ -0,0 +1,14 @@
1
+ export type { D3Selection } from "./src/D3Selection.js";
2
+ export { default as assign } from "./src/assign.js";
3
+ export { default as backgroundColor } from "./src/backgroundColor.js";
4
+ export { default as attrize } from "./src/attrize.js";
5
+ export { default as date } from "./src/date.js";
6
+ export { default as elem } from "./src/elem.js";
7
+ export { default as fontExists } from "./src/fontExists.js";
8
+ export { default as getSize } from "./src/getSize.js";
9
+ export { default as inViewport } from "./src/inViewport.js";
10
+ export { default as isObject } from "./src/isObject.js";
11
+ export { default as parseSides } from "./src/parseSides.js";
12
+ export { default as rtl } from "./src/rtl.js";
13
+ export { default as stylize } from "./src/stylize.js";
14
+ export { default as textWidth } from "./src/textWidth.js";
@@ -0,0 +1,28 @@
1
+ import { select } from "d3-selection";
2
+ import { transition } from "d3-transition";
3
+ /**
4
+ @type D3Selection
5
+ A permissive D3 selection type that accepts any generic parameterisation.
6
+ Uses `any` for the parent/datum generics because d3plus utility functions
7
+ (attrize, stylize, elem) must work with every combination.
8
+ */
9
+ export type D3Selection = ReturnType<typeof select<any, any>>;
10
+ /**
11
+ @type D3Transition
12
+ A D3 transition type derived from the return type of d3-transition's transition function.
13
+ */
14
+ export type D3Transition = ReturnType<typeof transition>;
15
+ /**
16
+ @interface Attrable
17
+ Anything that supports `.attr(name, value)` — both Selection and Transition.
18
+ */
19
+ export interface Attrable {
20
+ attr(name: string, value: string | number | boolean | null): this;
21
+ }
22
+ /**
23
+ @interface Stylable
24
+ Anything that supports `.style(name, value)` — both Selection and Transition.
25
+ */
26
+ export interface Stylable {
27
+ style(name: string, value: string | number | boolean | null): this;
28
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ A deeply recursive version of `Object.assign`.
3
+
4
+ @example <caption>this</caption>
5
+ assign({id: "foo", deep: {group: "A"}}, {id: "bar", deep: {value: 20}}));
6
+ @example <caption>returns this</caption>
7
+ {id: "bar", deep: {group: "A", value: 20}}
8
+ @param objects The source objects to merge into the target.
9
+ */
10
+ declare function assign(...objects: Record<string, unknown>[]): Record<string, unknown>;
11
+ export default assign;
@@ -0,0 +1,7 @@
1
+ import type { Attrable } from "./D3Selection.js";
2
+ /**
3
+ Applies each key/value in an object as an attr.
4
+ @param e The d3 selection to apply attributes to.
5
+ @param a An object of key/value attr pairs.
6
+ */
7
+ export default function (e: Attrable, a?: Record<string, string | number | boolean | null>): void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ Given a DOM element, returns its background color by walking up the
3
+ ancestor chain until a non-transparent background is found. Falls back
4
+ to "rgb(255, 255, 255)" (white) if every ancestor is transparent.
5
+ @param elem The DOM element to check.
6
+ */
7
+ export default function (elem: Element): string;
@@ -0,0 +1,5 @@
1
+ /**
2
+ Parses numbers and strings into valid JavaScript Date objects, supporting years, quarters, months, and ISO 8601 formats.
3
+ @param d The date value to parse (number, string, or Date).
4
+ */
5
+ export default function (d: number | string | false | undefined): Date | false | undefined;
@@ -0,0 +1,18 @@
1
+ import type { D3Selection } from "./D3Selection.js";
2
+ type AttrMap = Record<string, string | number | boolean | null>;
3
+ export interface ElemParams {
4
+ condition?: boolean;
5
+ enter?: AttrMap;
6
+ exit?: AttrMap;
7
+ duration?: number;
8
+ parent?: D3Selection;
9
+ transition?: D3Selection;
10
+ update?: AttrMap;
11
+ }
12
+ /**
13
+ Manages the enter/update/exit pattern for a single DOM element, applying enter, update, and exit attributes with optional transitions.
14
+ @param selector A CSS selector string for the element tag and classes.
15
+ @param p Configuration object with enter, exit, update, and parent options.
16
+ */
17
+ export default function (selector: string, p?: ElemParams): D3Selection;
18
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ Given either a single font-family or a list of fonts, returns the name of the first font that can be rendered, or `false` if none are installed on the user's machine.
3
+ @param font Can be either a valid CSS font-family string (single or comma-separated names) or an Array of string names.
4
+ */
5
+ declare const fontExists: (font: string | string[]) => string | false;
6
+ export default fontExists;
@@ -0,0 +1,5 @@
1
+ /**
2
+ Finds the available width and height for a specified HTMLElement, traversing it's parents until it finds something with constrained dimensions. Falls back to the inner dimensions of the browser window if none is found.
3
+ @private
4
+ */
5
+ export default function (elem: HTMLElement): [number | undefined, number | undefined];
@@ -0,0 +1,6 @@
1
+ /**
2
+ Determines whether a given DOM element is visible within the current viewport, with an optional pixel buffer.
3
+ @param elem The DOM element to check.
4
+ @param buffer Extra pixel margin around the viewport boundary.
5
+ */
6
+ export default function (elem: Element, buffer?: number): boolean;
@@ -0,0 +1,5 @@
1
+ /**
2
+ Detects if a variable is a javascript Object.
3
+ @param item The value to test.
4
+ */
5
+ export default function (item: unknown): boolean;
@@ -0,0 +1,11 @@
1
+ export interface ParsedSides {
2
+ top: number;
3
+ right: number;
4
+ bottom: number;
5
+ left: number;
6
+ }
7
+ /**
8
+ Converts a string of directional CSS shorthand values into an object with the values expanded.
9
+ @param sides The CSS shorthand string to expand.
10
+ */
11
+ export default function (sides: string | number): ParsedSides;
@@ -0,0 +1,5 @@
1
+ /**
2
+ @function prefix
3
+ @desc Returns the appropriate CSS vendor prefix, given the current browser.
4
+ */
5
+ export default function (): string;
@@ -0,0 +1,4 @@
1
+ /**
2
+ Returns `true` if the HTML or body element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl".
3
+ */
4
+ export default function (): boolean;
@@ -0,0 +1,7 @@
1
+ import type { Stylable } from "./D3Selection.js";
2
+ /**
3
+ Applies each key/value in an object as a style.
4
+ @param e The d3 selection to apply styles to.
5
+ @param s An object of key/value style pairs.
6
+ */
7
+ export default function (e: Stylable, s?: Record<string, string | number | boolean | null>): void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ Given a text string, returns the predicted pixel width of the string when placed into DOM.
3
+ @param text The text string to measure.
4
+ @param style CSS style properties to apply when measuring.
5
+ */
6
+ export default function (text: string, style?: Record<string, string | number>): number;
7
+ export default function (text: string[], style?: Record<string, string | number>): number[];