From 57ec6a67c4411813361d7e05ce5ade83a0854eb1 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 24 Feb 2024 15:46:34 +0100 Subject: [PATCH] feat!: directly pass through rough js options --- README.md | 3 ++- src/render.ts | 6 +++--- src/rough-notation.ts | 11 +++++++++++ src/types.ts | 6 +----- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fdc13df..ce6aec3 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Changes in this fork: - Expose config and data types - Support custom `class` option -- Support `overrides` option for customizing RoughJS +- Support passing through all options to RoughJS for full customization +- Support `getConfig` and `setConfig` methods - Improved build assets with `.cjs` and `.mjs` - `annotation.show()` returns a promise that resolves when the animation is done diff --git a/src/render.ts b/src/render.ts index 31cd47f..d525620 100644 --- a/src/render.ts +++ b/src/render.ts @@ -83,7 +83,7 @@ export function renderAnnotation( const animate = (config.animate === undefined) ? true : (!!config.animate) const iterations = config.iterations || 2 const rtl = config.rtl ? 1 : 0 - const o = getOptions('single', seed, config.overrides) + const o = getOptions('single', seed, config) switch (config.type) { case 'underline': { @@ -183,7 +183,7 @@ export function renderAnnotation( break } case 'circle': { - const doubleO = getOptions('double', seed, config.overrides) + const doubleO = getOptions('double', seed, config) const width = rect.w + (padding[1] + padding[3]) const height = rect.h + (padding[0] + padding[2]) const x = rect.x - padding[3] + (width / 2) @@ -199,7 +199,7 @@ export function renderAnnotation( break } case 'highlight': { - const o = getOptions('highlight', seed, config.overrides) + const o = getOptions('highlight', seed, config) strokeWidth = rect.h * 0.95 const y = rect.y + (rect.h / 2) for (let i = rtl; i < iterations + rtl; i++) { diff --git a/src/rough-notation.ts b/src/rough-notation.ts index d840132..3d5dea2 100644 --- a/src/rough-notation.ts +++ b/src/rough-notation.ts @@ -23,6 +23,17 @@ class RoughAnnotationImpl implements RoughAnnotation { this.attach() } + getConfig(key: T) { + return this._config[key] + } + + setConfig(key: T, value: RoughAnnotationConfig[T]) { + if (this._config[key] !== value) { + this._config[key] = value + this.refresh() + } + } + get animate() { return this._config.animate } set animate(value) { this._config.animate = value } diff --git a/src/types.ts b/src/types.ts index 27cc3f2..1c72698 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,7 +18,7 @@ export interface RoughAnnotationConfig extends RoughAnnotationConfigBase { rtl?: boolean } -export interface RoughAnnotationConfigBase { +export interface RoughAnnotationConfigBase extends Partial { animate?: boolean // defaults to true animationDuration?: number // defaults to 1000ms color?: string // defaults to currentColor @@ -30,10 +30,6 @@ export interface RoughAnnotationConfigBase { * Additional class to add to the root SVG element */ class?: string - /** - * RoughJS options - */ - overrides?: Partial } export interface RoughAnnotation extends RoughAnnotationConfigBase {