Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added iframe component #237

Merged
merged 9 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/docs/public/components/iframe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/streamsync/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,14 @@ def _transform_page_open(self, ev) -> str:
payload = str(ev.payload)
return payload

def _transform_load(self, ev) -> Dict:
payload = ev.payload
url = payload.get("url")
tf_payload = {
"url": url,
}
return tf_payload

def _transform_change(self, ev) -> str:
payload = str(ev.payload)
return payload
Expand Down
13 changes: 13 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ def test_hashchange(self) -> None:
},
}

def test_load(self) -> None:
ev = StreamsyncEvent(
type="ss-load",
instancePath=self.root_instance_path,
payload={
"url": "/myPage",
}
)
self.ed.transform(ev)
assert ev.payload == {
"url": "/myPage",
}

def test_webcam(self) -> None:
ev = StreamsyncEvent(
type="ss-webcam",
Expand Down
2 changes: 2 additions & 0 deletions ui/src/core/templateMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import CoreSeparator from "../core_components/CoreSeparator.vue";
import CoreTab from "../core_components/CoreTab.vue";
import CoreTabs from "../core_components/CoreTabs.vue";
import CoreImage from "../core_components/CoreImage.vue";
import CoreIFrame from "../core_components/CoreIFrame.vue";
import CoreTimer from "../core_components/CoreTimer.vue";
import CoreWebcamCapture from "../core_components/CoreWebcamCapture.vue";
import CoreVegaLiteChart from "../core_components/CoreVegaLiteChart.vue";
Expand Down Expand Up @@ -64,6 +65,7 @@ const templateMap = {
horizontalstack: CoreHorizontalStack,
separator: CoreSeparator,
image: CoreImage,
iframe: CoreIFrame,
icon: CoreIcon,
timer: CoreTimer,
textinput: CoreTextInput,
Expand Down
106 changes: 106 additions & 0 deletions ui/src/core_components/CoreIFrame.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<template>
<div class="CoreIFrame" v-on:click="handleClick" ref="rootEl">
<iframe
@load="handleLoad"
:src="fields.src.value"
draggable="false"
/>
<div class="mask" />
raaymax marked this conversation as resolved.
Show resolved Hide resolved
</div>
</template>

<script lang="ts">
import { FieldCategory, FieldType } from "../streamsyncTypes";
import { cssClasses, secondaryTextColor } from "../renderer/sharedStyleFields";
import { getClick } from "../renderer/syntheticEvents";

const description = "A component to embed an external resource in an iframe.";

const loadHandlerStub = `
def load_handler(state):

# Sets status message when resource is loaded

state["status"] = "Page loaded"`;

export default {
streamsync: {
name: "IFrame",
description,
category: "Content",
raaymax marked this conversation as resolved.
Show resolved Hide resolved
fields: {
src: {
name: "Source",
default: '',
desc: "A valid URL. Alternatively, you can provide a state reference to a Matplotlib figure or a packed file.",
raaymax marked this conversation as resolved.
Show resolved Hide resolved
type: FieldType.Text,
},
cssClasses,
},
events: {
"ss-load": {
desc: "Fires when the resource has successfully loaded.",
stub: loadHandlerStub.trim(),
},
},
},
};
</script>

<script setup lang="ts">
import { Ref, computed, inject, ref } from "vue";
import injectionKeys from "../injectionKeys";

const rootEl:Ref<HTMLElement> = ref(null);
const fields = inject(injectionKeys.evaluatedFields);

function handleClick(ev: MouseEvent) {
raaymax marked this conversation as resolved.
Show resolved Hide resolved
const ssEv = getClick(ev);
rootEl.value.dispatchEvent(ssEv);
}
function handleLoad(ev) {
console.log(ev.target.src);
raaymax marked this conversation as resolved.
Show resolved Hide resolved
const payload = {
url: ev.target.src,
};
const event = new CustomEvent("ss-load", {
detail: {
payload,
},
});
rootEl.value.dispatchEvent(event);
}
</script>

<style scoped>
@import "../renderer/sharedStyles.css";

.CoreIFrame {
position: relative;
width: 100%;
height: 80vh;
}

.CoreIFrame iframe {
width: 100%;
height: 100%;
display: block;
margin: auto;
border-radius: 12px;
raaymax marked this conversation as resolved.
Show resolved Hide resolved
border: 1px solid var(--separatorColor);
}

.CoreIFrame .mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0);
border-radius: 12px;
raaymax marked this conversation as resolved.
Show resolved Hide resolved
}

.CoreIFrame.selected .mask {
display: none;
}
</style>