refactor: consolidate core modules into utils.ts
Merge path-style.ts and with-props.ts into utils.ts to reduce the number of core files from 5 to 3 (utils.ts, props.ts, register.ts). Unify duplicate ../core/utils.ts imports in all component files. Remove stale re-exports from src/index.ts.main
parent
d8bdc2dcfa
commit
7ae092ac98
@ -1,38 +0,0 @@
|
||||
import { Path } from 'leaflet';
|
||||
import { parseAttributeValue } from './utils.ts';
|
||||
|
||||
// The set of HTML attribute names that map to Leaflet Path style options.
|
||||
// These are handled especially because Leaflet exposes them through
|
||||
// setStyle() rather than individual setter methods, and they are shared
|
||||
// across many vector components (polyline, polygon, circle, rectangle).
|
||||
const PATH_STYLE_ATTRS = new Set([
|
||||
'color',
|
||||
'weight',
|
||||
'opacity',
|
||||
'fill',
|
||||
'fill-color',
|
||||
'fill-opacity',
|
||||
'stroke',
|
||||
'dash-array',
|
||||
'dash-offset',
|
||||
'line-cap',
|
||||
'line-join',
|
||||
'fill-rule',
|
||||
]);
|
||||
|
||||
// Quick check for whether an attribute name is a path-style attribute.
|
||||
// Used in attributeChangedCallback to decide whether to route through
|
||||
// setLayerAttr or updatePathStyle.
|
||||
export function isPathStyleAttr(name: string): boolean {
|
||||
return PATH_STYLE_ATTRS.has(name);
|
||||
}
|
||||
|
||||
// Applies a single style change to a Leaflet Path by converting the
|
||||
// kebab-case attribute name to camelCase and calling setStyle() with
|
||||
// the parsed value. This is called instead of the generic setLayerAttr
|
||||
// dispatcher because Path.setStyle is a bulk-update method that merges
|
||||
// into the existing style object rather than replacing it.
|
||||
export function updatePathStyle(obj: Path, name: string, value: string | null) {
|
||||
const key = name.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
|
||||
obj.setStyle({ [key]: parseAttributeValue(value) });
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
import type { PropDef, PropTypesFromTable } from './props.ts';
|
||||
import { definePropAccessors } from './utils.ts';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type Ctor<T = object> = new (...args: any[]) => T;
|
||||
|
||||
// Mixin factory that turns a PROPS table into reactive property
|
||||
// accessors + observedAttributes in one shot. Every component that
|
||||
// follows the PROPS-table pattern uses this instead of manually
|
||||
// defining a static initialization block, getters/setters, and an
|
||||
// observedAttributes getter.
|
||||
//
|
||||
// Usage: `class LeafletFoo extends WithProps(HTMLElement, PROPS)`
|
||||
// Returns an intersection type so consumers see the generated
|
||||
// properties from PropTypesFromTable.
|
||||
export function WithProps<TBase extends Ctor<HTMLElement>, TProps extends Record<string, PropDef>>(
|
||||
Base: TBase,
|
||||
props: TProps,
|
||||
): TBase & Ctor<HTMLElement & PropTypesFromTable<TProps>> {
|
||||
class WithProps extends Base {
|
||||
static {
|
||||
definePropAccessors(WithProps.prototype, props);
|
||||
}
|
||||
static get observedAttributes(): string[] {
|
||||
return Object.values(props).map((s) => s.attr);
|
||||
}
|
||||
}
|
||||
|
||||
return WithProps as TBase & Ctor<HTMLElement & PropTypesFromTable<TProps>>;
|
||||
}
|
||||
Loading…
Reference in New Issue