Skip to content

Commit

Permalink
fix: Allow dynamic styles using Data Key
Browse files Browse the repository at this point in the history
(cherry picked from commit 587fd04)
  • Loading branch information
surajshetty3416 authored and mergify[bot] committed May 29, 2024
1 parent c5630de commit 196f79f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion builder/builder/doctype/builder_page/builder_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_context(self, context):
content, style, fonts = get_block_html(blocks)
context.fonts = fonts
context.content = content
context.style = style
context.style = render_template(style, page_data)
builder_path = frappe.conf.builder_path or "builder"
context.editor_link = f"/{builder_path}/page/{self.name}"
context.base_url = frappe.utils.get_url(".")
Expand All @@ -145,6 +145,7 @@ def get_context(self, context):
context.update(page_data)
self.set_meta_tags(context=context, page_data=page_data)
self.set_favicon(context)

try:
context["content"] = render_template(context.content, context)
except TemplateSyntaxError:
Expand Down
26 changes: 21 additions & 5 deletions frontend/src/components/BuilderBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
:breakpoint="breakpoint"
:editable="isEditable"
:isSelected="isSelected"
:target="(target as HTMLElement)" />
:target="target as HTMLElement" />
</teleport>
</template>
<script setup lang="ts">
Expand Down Expand Up @@ -137,7 +137,23 @@ const target = computed(() => {
});
const styles = computed(() => {
return { ...props.block.getStyles(props.breakpoint), ...props.block.getEditorStyles() } as BlockStyleMap;
let dynamicStyles = {};
if (props.data) {
if (props.block.getDataKey("type") === "style") {
dynamicStyles = {
[props.block.getDataKey("property") as string]: getDataForKey(
props.data,
props.block.getDataKey("key"),
),
};
}
}
return {
...props.block.getStyles(props.breakpoint),
...props.block.getEditorStyles(),
...dynamicStyles,
} as BlockStyleMap;
});
const loadEditor = computed(() => {
Expand Down Expand Up @@ -165,7 +181,7 @@ onMounted(async () => {
useDraggableBlock(
props.block,
component.value as HTMLElement,
reactive({ ghostScale: canvasProps?.scale || 1 })
reactive({ ghostScale: canvasProps?.scale || 1 }),
);
}
});
Expand Down Expand Up @@ -264,7 +280,7 @@ if (!props.preview) {
} else if (oldValue === props.block.blockId) {
isHovered.value = false;
}
}
},
);
watch(
() => store.activeCanvas?.selectedBlockIds,
Expand All @@ -278,7 +294,7 @@ if (!props.preview) {
{
deep: true,
immediate: true,
}
},
);
}
</script>
6 changes: 4 additions & 2 deletions frontend/src/utils/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { addPxToNumber, getBlockCopy, getNumberFromPx, getTextContent, kebabToCa

export type styleProperty = keyof CSSProperties;

type BlockDataKeyType = "key" | "attribute" | "style";

export interface BlockDataKey {
key?: string;
type?: string;
type?: BlockDataKeyType;
property?: string;
}

Expand Down Expand Up @@ -528,7 +530,7 @@ class Block implements BlockOptions {
}
return dataKey;
}
setDataKey(key: keyof BlockDataKey, value: string) {
setDataKey(key: keyof BlockDataKey, value: BlockDataKeyType) {
if (!this.dataKey) {
this.dataKey = {
key: "",
Expand Down

0 comments on commit 196f79f

Please sign in to comment.