From 61d5862edfc4b66b6566256745599aa115006fdd Mon Sep 17 00:00:00 2001 From: Carlos Rufo Date: Wed, 12 Mar 2025 08:22:47 -0700 Subject: [PATCH] feat: add customizable styles for drag guides and containers in ComponentDrag --- .../core/src/commands/view/ComponentDrag.ts | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/core/src/commands/view/ComponentDrag.ts b/packages/core/src/commands/view/ComponentDrag.ts index f5294e764..089d147ff 100644 --- a/packages/core/src/commands/view/ComponentDrag.ts +++ b/packages/core/src/commands/view/ComponentDrag.ts @@ -31,6 +31,16 @@ type Opts = { onStart?: (data: any) => Editor; onDrag?: (data: any) => Editor; onEnd?: (ev: Event, opt: any, data: any) => void; + styles?: { + guides?: { + active?: { color?: string }; + inactive?: { color?: string }; + }; + guidesContainer?: { + line?: HTMLElement['style']; + content?: HTMLElement['style']; + }; + }; }; const evName = 'dmode'; @@ -63,7 +73,22 @@ export default { ...dragger, }; this.setupGuides(); - this.opts = opts; + this.opts = { + ...opts, + // debug: true + styles: { + guides: { + active: { color: 'green' }, + inactive: { color: 'red' }, + }, + guidesContainer: { + line: { 'background-color': 'blue' }, + content: { color: 'blue' }, + }, + ...opts.styles, + }, + }; + // this.opts = opts; this.editor = editor; this.em = editor.getModel(); this.target = target; @@ -116,8 +141,11 @@ export default { const pfx = editor.getConfig().stylePrefix; const elInfoX = document.createElement('div'); const elInfoY = document.createElement('div'); - const guideContent = `
-
+ const guideInfoLineStyles = this.opts?.styles?.guidesContainer?.line; + const guideInfoContentStyles = this.opts?.styles?.guidesContainer?.content; + + const guideContent = `
+
`; guidesEl = document.createElement('div'); guidesEl.className = `${pfx}guides`; @@ -205,11 +233,15 @@ export default { const un = 'px'; const guideSize = item.active ? 2 : 1; let numEl = el.children[0]; - el.style = `position: absolute; background-color: ${item.active ? 'green' : 'red'};`; + + const colorActive = this.opts?.styles?.guides?.active?.color ?? 'green'; + const colorInactive = this.opts?.styles?.guides?.inactive?.color ?? 'red'; + + el.style = `position: absolute; background-color: ${item.active ? colorActive : colorInactive};`; if (!el.children.length) { numEl = document.createElement('div'); - numEl.style = 'position: absolute; color: red; padding: 5px; top: 0; left: 0;'; + numEl.style = `position: absolute; color: ${colorInactive}; padding: 5px; top: 0; left: 0;`; el.appendChild(numEl); } @@ -488,3 +520,9 @@ export default { [k: string]: any; } >; + +function styleObjectToString(style: HTMLElement['style']): string { + return Object.entries(style) + .map(([key, value]) => `${key}: ${value}`) + .join('; '); +}