Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22414009
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/public/pin-icon.svg b/public/pin-icon.svg
new file mode 100644
index 0000000..7e0dd58
--- /dev/null
+++ b/public/pin-icon.svg
@@ -0,0 +1,4 @@
+<svg width="29" height="41" viewBox="0 0 29 41" fill="none" xmlns="http://www.w3.org/2000/svg">
+<ellipse cx="14.5" cy="15.4219" rx="7" ry="7.5" fill="white"/>
+<path d="M28.4657 14.2917C28.0051 7.02103 21.6168 0.921875 14.4905 0.921875H14.3875C10.6918 0.953431 7.21825 2.46022 4.60475 5.1691C1.95692 7.90755 0.5 11.5404 0.5 15.3981C0.5 27.7434 13.1728 40.0827 13.7115 40.6014C13.9336 40.8154 14.2178 40.9219 14.501 40.9219C14.7841 40.9219 15.0673 40.8154 15.2885 40.6014C15.8416 40.0669 28.5 27.7414 28.5 15.4031C28.5 15.0313 28.4895 14.6615 28.4657 14.2927V14.2917ZM14.501 21.9065C11.0293 21.9065 8.2051 18.9856 8.2051 15.3952C8.2051 11.8047 11.0293 8.88381 14.501 8.88381C17.9726 8.88381 20.7949 11.8047 20.7949 15.3952C20.7949 18.9856 17.9707 21.9065 14.501 21.9065Z" fill="#CADAE4"/>
+</svg>
diff --git a/src/back/jdd-components/components.ts b/src/back/jdd-components/components.ts
index f34e64b..35cee56 100644
--- a/src/back/jdd-components/components.ts
+++ b/src/back/jdd-components/components.ts
@@ -1,10 +1,13 @@
// DO NOT EDIT! This file is generated automaticaly with 'npm run generate-components'
import { Registry } from "@sealcode/jdd";
export const registry = new Registry();
import { ImageDemo } from "./image-demo/image-demo.jdd.js";
registry.add("image-demo", ImageDemo);
+import { MapWithPins } from "./map-with-pins/map-with-pins.jdd.js";
+registry.add("map-with-pins", MapWithPins);
+
import { NiceBox } from "./nice-box/nice-box.jdd.js";
registry.add("nice-box", NiceBox);
diff --git a/src/back/jdd-components/map-with-pins/map-with-pins.css b/src/back/jdd-components/map-with-pins/map-with-pins.css
new file mode 100644
index 0000000..4fb7421
--- /dev/null
+++ b/src/back/jdd-components/map-with-pins/map-with-pins.css
@@ -0,0 +1,50 @@
+.map-with-pins {
+ #map {
+ height: 534px;
+ }
+ .popup {
+ .leaflet-popup-content-wrapper,
+ .leaflet-popup-tip {
+ background: #0d4d69;
+ color: #ffffff;
+ text-align: center;
+ }
+ .leaflet-popup-content {
+ margin: 0;
+ }
+ .leaflet-popup-content-wrapper {
+ padding: 22px;
+ }
+ }
+
+ .title,
+ .address {
+ margin: 0 0 8px 0;
+ }
+
+ .title {
+ text-transform: uppercase;
+ font-size: 22px;
+ line-height: 26px;
+ }
+
+ .address {
+ font-size: 12px;
+ line-height: 21px;
+ }
+ .button {
+ display: inline-block;
+ margin: 0 auto;
+ text-align: center;
+ text-decoration: none;
+ border-radius: 25px;
+ text-transform: uppercase;
+ padding: 4px 25px;
+ border: 1px solid #ffffff;
+ color: #ffffff;
+ min-width: 170px;
+ line-height: 18px;
+ font-size: 12px;
+ background-color: #0d4d69;
+ }
+}
diff --git a/src/back/jdd-components/map-with-pins/map-with-pins.jdd.tsx b/src/back/jdd-components/map-with-pins/map-with-pins.jdd.tsx
new file mode 100644
index 0000000..27bac7b
--- /dev/null
+++ b/src/back/jdd-components/map-with-pins/map-with-pins.jdd.tsx
@@ -0,0 +1,126 @@
+import { FlatTemplatable, TempstreamJSX } from "tempstream";
+import {
+ Component,
+ ComponentArguments,
+ ExtractStructuredComponentArgumentsValues,
+ JDDContext,
+} from "@sealcode/jdd";
+
+const coordinates = new ComponentArguments.ShortText();
+coordinates.setExampleValues([
+ "52.39706859245613, 16.898251247012197",
+ "52.8, 16.6",
+ "52.5, 16.1",
+ "52.1, 16.35",
+]);
+
+const component_arguments = {
+ pins: new ComponentArguments.List(
+ new ComponentArguments.Structured({
+ title: new ComponentArguments.ShortText(),
+ address: new ComponentArguments.ShortText(),
+ coordinates,
+ button: new ComponentArguments.Structured({
+ text: new ComponentArguments.ShortText(),
+ link: new ComponentArguments.ShortText(),
+ }),
+ })
+ ),
+} as const;
+
+export class MapWithPins extends Component<typeof component_arguments> {
+ getArguments() {
+ return component_arguments;
+ }
+
+ toHTML(
+ { pins }: ExtractStructuredComponentArgumentsValues<typeof component_arguments>,
+ { render_markdown }: JDDContext
+ ): FlatTemplatable {
+ return (
+ <div class="map-with-pins">
+ <link
+ rel="stylesheet"
+ href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
+ integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
+ crossorigin=""
+ />
+ <script
+ onload="loadMap()"
+ src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
+ integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
+ crossorigin=""
+ ></script>
+ {
+ /* HTML */ `<script>
+ function loadMap() {
+ const mapDiv = document.getElementById("map");
+ const resizeObserver = new ResizeObserver(() => {
+ map.invalidateSize();
+ });
+
+ resizeObserver.observe(mapDiv);
+ var map = L.map("map");
+ L.tileLayer(
+ "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
+ {
+ maxZoom: 19,
+ attribution:
+ '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
+ }
+ ).addTo(map);
+
+ const pins = ${JSON.stringify(pins)};
+ pins.forEach((pin) => addPin(pin, map));
+ }
+
+ function addPin(pin, map) {
+ var pinIcon = L.icon({
+ iconUrl: "/pin-icon.svg",
+ iconSize: [29, 41],
+ iconAnchor: [14, 40],
+ popupAnchor: [-3, 14],
+ });
+
+ var marker = L.marker(
+ pin.coordinates.split(", ").map((x) => parseFloat(x)),
+ {
+ icon: pinIcon,
+ }
+ ).addTo(map);
+
+ var popup = L.popup({
+ closeButton: false,
+ autoClose: false,
+ closeOnEscapeKey: false,
+ closeOnClick: false,
+ className: "popup",
+ offset: [0, -32],
+ maxWidth: "auto",
+ })
+ .setLatLng(
+ pin.coordinates.split(", ").map((x) => parseFloat(x))
+ )
+ .setContent(
+ /* HTML */ \`<div class="popup-content">
+ <p class="title">\${pin.title}</p>
+ <p class="address">\${pin.address}</p>
+ <a class="button" href="\${pin.button.link}">
+ \${pin.button.text}
+ </a>
+ </div> \`
+ )
+ .addTo(map);
+ map.setView(
+ pin.coordinates.split(", ").map((x) => parseFloat(x)),
+ 13
+ );
+ }
+ </script>`
+ }
+
+ <div id="map"></div>
+ </div>
+ );
+ }
+}
diff --git a/src/back/routes/components.sreact.tsx b/src/back/routes/components.sreact.tsx
index 29bb709..6c97f6b 100644
--- a/src/back/routes/components.sreact.tsx
+++ b/src/back/routes/components.sreact.tsx
@@ -1,341 +1,341 @@
import {
ComponentArgument,
List,
render,
simpleJDDContext,
Structured,
} from "@sealcode/jdd";
import { StateAndMetadata, StatefulPage, to_base64 } from "@sealcode/sealgen";
import { hasFieldOfType, hasShape, is, predicates } from "@sealcode/ts-predicates";
import { BaseContext } from "koa";
import { Templatable, TempstreamJSX } from "tempstream";
import html from "../html.js";
import { registry } from "../jdd-components/components.js";
import { ComponentInput } from "./component-preview/component-input.js";
import { ComponentPreviewActions } from "./component-preview/component-preview-actions.js";
import { jdd_context } from "./jdd-context.js";
export const actionName = "Components";
function id<X>(_: any, __: any, x: X): X {
return x;
}
async function encodeSealiousFile(maybe_file: Record<string, unknown>) {
if (maybe_file?.getDataPath) {
return simpleJDDContext.encode_file(
{
type: "path",
// asserting that this is an instance of sealious' FileFromPath
path: (maybe_file as unknown as { data: { path: string } }).data
.path as string,
},
false
);
}
}
const componentArgToRequestProcessor = {
list: async function (arg, arg_name, value: unknown) {
if (
!is(value, predicates.array(predicates.object)) &&
!is(value, predicates.object)
) {
throw new Error(`$.${arg_name} is not a list or object`);
}
const values = Array.isArray(value) ? value : Object.values(value);
const nested_arg_type = (arg as List<ComponentArgument<unknown>>).item_type;
let array_result: Array<unknown> = await Promise.all(
values.map((value, index) => {
return (
componentArgToRequestProcessor[nested_arg_type.getTypeName()] || id
)(nested_arg_type, `${arg_name}[${index}]`, value);
})
);
if (nested_arg_type.getTypeName() != "list") {
array_result = array_result.flat();
}
return array_result;
},
structured: async function (arg, arg_name, value) {
if (!is(value, predicates.object)) {
throw new Error(`${arg_name} is not an object`);
}
let result = Object.fromEntries(
await Promise.all(
Object.entries(value).map(async ([obj_key, obj_value]) => {
const nested_arg_type: ComponentArgument<unknown> = (
arg as Structured<Record<string, ComponentArgument<unknown>>>
).structure[obj_key];
if (!nested_arg_type) {
return [obj_key, null];
}
const new_value = await (
componentArgToRequestProcessor[nested_arg_type.getTypeName()] ||
id
)(arg, `${arg_name}[${obj_key}]`, obj_value);
return [obj_key, new_value];
})
)
);
// if we're in a list and any of the values return an array, we will multiply the object
if (arg.hasParent("list")) {
const keys_with_unexpected_arrays = Object.entries(result)
.filter(([key, value]) => {
const nested_arg_type: ComponentArgument<unknown> = (
arg as Structured<Record<string, ComponentArgument<unknown>>>
).structure[key];
return (
nested_arg_type.getTypeName() !== "list" && Array.isArray(value)
);
})
.map(([key]) => key);
if (keys_with_unexpected_arrays.length > 1) {
throw new Error(
"Multiplying on multiple fields at the same time is not implemented yet"
);
}
if (keys_with_unexpected_arrays.length == 1) {
const key = keys_with_unexpected_arrays[0];
const old_result = result;
result = (old_result[key] as Array<unknown>).map((value) => ({
...old_result,
[key]: value,
}));
}
}
return result;
},
image: async function (arg, _, value: unknown) {
if (
!hasShape(
{
new: predicates.maybe(predicates.array(predicates.object)),
old: predicates.string,
},
value
)
) {
return null;
}
const files = (value.new || []).filter((e) => e);
if (files.length == 0) {
return value.old;
} else if (files.length == 1) {
return encodeSealiousFile(files[0]);
} else if (arg.hasParent("list")) {
return Promise.all(files.map(encodeSealiousFile));
}
},
} as Record<
string,
(arg: ComponentArgument<any>, arg_name: string, value: unknown) => Promise<unknown>
>;
export type ComponentPreviewState = {
component: string;
component_args: Record<string, unknown>;
};
export default new (class ComponentsPage extends StatefulPage<
ComponentPreviewState,
typeof ComponentPreviewActions
> {
actions = ComponentPreviewActions;
async getInitialState() {
const [component_name, component] = Object.entries(registry.getAll())[0];
const initial_state = {
component: component_name,
component_args: await component.getExampleValues(jdd_context),
};
return initial_state;
}
wrapInLayout(ctx: BaseContext, content: Templatable): Templatable {
return html(ctx, "Components", content, {
- morphing: true,
+ morphing: false,
preserveScroll: true,
autoRefreshCSS: true,
navbar: () => ``,
});
}
wrapInForm(state: ComponentPreviewState, content: Templatable): Templatable {
// overwriting this method in order to add enctype to form
return (
<form action="./" method="POST" enctype="multipart/form-data">
<input
name="state"
type="hidden"
value={to_base64(JSON.stringify(state))}
/>
{content}
</form>
);
}
async preprocessRequestBody<
T extends StateAndMetadata<ComponentPreviewState, typeof ComponentPreviewActions>
>(values: Record<string, unknown>): Promise<T> {
let old_component = hasFieldOfType(values, "component", predicates.string)
? values.component
: null;
const new_component = hasShape(
{ $: predicates.shape({ component: predicates.string }) },
values
)
? values.$.component
: null;
const component_name = new_component || old_component;
if (!component_name) {
throw new Error("Unspecified component name");
}
const component = registry.get(component_name);
if (!component) {
throw new Error(`Unknown component: ${component_name}`);
}
if (
!hasShape(
{ $: predicates.shape({ component_args: predicates.object }) },
values
)
) {
// no component args to overwrite
return values as T;
}
for (const [arg_name, arg] of Object.entries(component.getArguments())) {
let value = values.$.component_args[arg_name];
if (value) {
const new_value = await (
componentArgToRequestProcessor[arg.getTypeName()] || id
)(arg, arg_name, value);
values.$.component_args[arg_name] = new_value;
}
}
return values as T;
}
render(
ctx: BaseContext,
state: ComponentPreviewState,
inputs: Record<string, string>
) {
const all_components = registry.getAll();
const component =
registry.get(state.component) || Object.values(all_components)[0];
return (
<div class="two-column" id="component-debugger">
<div class="resizable">
{/*The below button has to be here in order for it to be the default behavior */}
<input type="submit" value="Preview" />
<select
name="component"
onchange={this.makeActionCallback("change_component")}
autocomplete="off"
>
{Object.entries(all_components).map(([name]) => (
<option value={name} selected={name == state.component}>
{name}
</option>
))}
</select>
{this.makeActionButton(state, "randomize_args")}
<fieldset class="component-preview-parameters">
<legend>Parameters</legend>
{Object.entries(component.getArguments()).map(
async ([arg_name, arg]) => (
<ComponentInput
{...{
state,
arg_path: [arg_name],
arg,
value:
state.component_args[arg_name] === undefined
? await arg.getExampleValue(jdd_context)
: state.component_args[arg_name],
onblur: this.rerender(),
page: this,
}}
/>
)
)}
<input type="submit" value="Preview" />
</fieldset>
<code>{JSON.stringify(state)}</code>
</div>
<div class="resize-gutter"></div>
{
/* HTML */ `<script>
(function () {
let is_resizing = false;
let origin_x;
let origin_width;
const gutter = document.querySelector(".resize-gutter");
const resizable = document.querySelector(".resizable");
const move_listener = (e) => {
const new_width = Math.max(
origin_width + (e.clientX - origin_x),
1
);
document.documentElement.style.setProperty(
"--resizable-column-width",
new_width + "px"
);
};
gutter.addEventListener("mousedown", (e) => {
is_resizing = true;
origin_x = e.clientX;
origin_width = resizable.getBoundingClientRect().width;
document.addEventListener("mousemove", move_listener);
document.addEventListener("mouseup", () => {
document.removeEventListener(
"mousemove",
move_listener
);
});
e.preventDefault();
});
})();
</script>`
}
<div class="component-preview">
<fieldset>
<legend>Preview</legend>
{render(
registry,
[
{
component_name: state.component,
args: state.component_args,
},
],
jdd_context
)}
</fieldset>
</div>
{
/* HTML */ `<script>
const main_form = document
.querySelector("#component-debugger")
.closest("form");
document.documentElement.addEventListener("ts-rebuilt", () => {
main_form.requestSubmit();
});
main_form.addEventListener("turbo:submit-end", () => {
// this clears the values of file inputs, so they don't get unecessarily
// re-uploaded on future submissions - the file is alreade there on the server
main_form
.querySelectorAll("input[type=file]")
.forEach((input) => (input.value = ""));
});
</script>`
}
</div>
);
}
})();
diff --git a/src/includes.css b/src/includes.css
index c56a2d5..047eef4 100644
--- a/src/includes.css
+++ b/src/includes.css
@@ -1,8 +1,9 @@
/* DO NOT EDIT! This file is generated automaticaly with npx sealgen generate-css-includes */
@import "../node_modules/@sealcode/sealgen/src/forms/forms.css";
@import "back/jdd-components/image-demo/image-demo.css";
+@import "back/jdd-components/map-with-pins/map-with-pins.css";
@import "back/routes/common/ui/input.css";
@import "back/routes/components.css";
@import "colors.css";
@import "tables.css";
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 28, 01:13 (5 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229901
Default Alt Text
(17 KB)
Attached To
Mode
rAPROXY Assetproxy
Attached
Detach File
Event Timeline