This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: plugin system, refactoring visualizer (#50)
* refactor visualizer * add Plugin component * enable plugins in visualizer * update config * format * change default value of options for show method * fix storytelling * add dnd provider to published page * fix property value conversion * fix property value convertion bug * update gql * refactor storytelling * update react-svg * perf icon * update why-did-you-render * set up why-did-you-render * perf treeview hooks * fix errors from react * refactor use-delyaed-count * add primitive selection reason * fix fov animation * add selection reason * cancel current delay in use-delayed-count * refactor and fix photooverlay * fix type error * temporally disable primitive and widget plugin * reduce load of plugin system * make plugin block selectable
- Loading branch information
Showing
169 changed files
with
7,078 additions
and
4,151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const html = ` | ||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" /> | ||
<script id="l" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script> | ||
<div id="map" style="width: 100%; height: 300px;"></div> | ||
<script> | ||
document.getElementById("l").addEventListener("load", () => { | ||
const map = L.map("map").setView([0, 0], 13); | ||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | ||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
}).addTo(map); | ||
const marker = L.marker(); | ||
const cb = (block) => { | ||
if (block?.property?.location) { | ||
const latlng = [block.property.location.lat, block.property.location.lng]; | ||
map.setView(latlng); | ||
marker.setLatLng(latlng).addTo(map); | ||
} else { | ||
marker.remove(); | ||
} | ||
parent.postMessage("updated", "*"); | ||
}; | ||
addEventListener("message", e => { | ||
if (e.source !== parent) return; | ||
cb(e.data); | ||
}); | ||
cb(${JSON.stringify(reearth.block)}); | ||
}); | ||
</script> | ||
`; | ||
|
||
reearth.ui.show(html); | ||
reearth.on("update", () => { | ||
reearth.ui.postMessage(reearth.block); | ||
}); | ||
reearth.on("message", msg => { | ||
console.log("message received:", msg); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const html = ` | ||
<script> | ||
addEventListener("message", e => { | ||
if (e.source !== parent) return; | ||
const p = document.createElement("p"); | ||
p.textContent = JSON.stringify(e.data); | ||
document.body.appendChild(p); | ||
console.log("plugin -> iframe", e.data); | ||
parent.postMessage(e.data, "*"); | ||
}); | ||
</script> | ||
`; | ||
|
||
reearth.ui.show(html, { visible: false }); | ||
reearth.on("message", (message) => { | ||
console.log("plugin <- iframe", message); | ||
}); | ||
reearth.ui.postMessage("foo!"); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const html = ` | ||
<h1>IFrame works</h1> | ||
<script> | ||
addEventListener("message", e => { | ||
if (e.source !== parent) return; | ||
const p = document.createElement("p"); | ||
p.textContent = JSON.stringify(e.data); | ||
document.body.appendChild(p); | ||
console.log("plugin -> iframe", e.data); | ||
parent.postMessage(e.data, "*"); | ||
}); | ||
</script> | ||
`; | ||
|
||
reearth.ui.show(html); | ||
reearth.on("message", (message) => { | ||
console.log("plugin <- iframe", message); | ||
}); | ||
reearth.ui.postMessage("foo!"); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// import { writeFileSync } from "fs"; | ||
import * as ts from "typescript"; | ||
import tsconfig from "../tsconfig.json"; | ||
|
||
const files = ["./src/plugin/api.ts"]; | ||
const program = ts.createProgram(files, tsconfig as any); | ||
const tc = program.getTypeChecker(); | ||
|
||
type D = { | ||
name: string; | ||
desc: string; | ||
properties: P[]; | ||
}; | ||
|
||
type P = { | ||
name: string; | ||
desc: string; | ||
type: string; | ||
required: boolean; | ||
}; | ||
|
||
for (const s of program.getSourceFiles()) { | ||
if (s.fileName.substr(-5) === ".d.ts") continue; | ||
const decls: D[] = []; | ||
ts.forEachChild(s, node => { | ||
const d = parse(node); | ||
if (d) { | ||
decls.push(d); | ||
} | ||
}); | ||
const res = render(decls, n => decls.findIndex(d => d.name === n) != -1); | ||
// writeFileSync("./docs/plugin.md", res); | ||
console.log(res); | ||
} | ||
|
||
function parse(node: ts.Node): D | undefined { | ||
if (!ts.isTypeAliasDeclaration(node)) return; | ||
const sym = tc.getSymbolAtLocation(node.name); | ||
if (!sym) return; | ||
|
||
const properties = tc | ||
.getTypeAtLocation(node) | ||
.getApparentProperties() | ||
.map((s): P | undefined => { | ||
const d = s.getDeclarations()?.[0]; | ||
const t = d ? tc.getTypeAtLocation(d) : undefined; | ||
if (!t) return undefined; | ||
return { | ||
name: s.name, | ||
type: tc.typeToString(t), | ||
required: (s.flags & ts.SymbolFlags.Optional) === 0, | ||
desc: ts.displayPartsToString(s.getDocumentationComment(tc)), | ||
}; | ||
}) | ||
.filter((p): p is P => !!p); | ||
|
||
return { | ||
name: node.name.text, | ||
desc: ts.displayPartsToString(sym.getDocumentationComment(tc)), | ||
properties, | ||
}; | ||
} | ||
|
||
function render(decls: D[], linkable: (name: string) => boolean): string { | ||
return decls | ||
.flatMap(d => [ | ||
`# ${d.name}`, | ||
d.desc, | ||
d.properties.map(p => `- ${renderHead(p, linkable)}`).join("\n"), | ||
...d.properties.filter(p => p.desc).flatMap(p => [`## ${renderHead(p, linkable)}`, p.desc]), | ||
]) | ||
.filter(Boolean) | ||
.join("\n\n"); | ||
} | ||
|
||
function renderHead(p: P, linkable: (name: string) => boolean): string { | ||
return [p.name, ": ", renderType(p.type, p.required, linkable)].join(""); | ||
} | ||
|
||
function renderType(type: string, required: boolean, linkable: (name: string) => boolean) { | ||
const ts = split(type, [") => ", "[]", "?: ", ": ", " & ", " | ", "(", ")", ", "]) | ||
.map(s => | ||
typeof s === "string" && linkable(s) | ||
? { | ||
link: s, | ||
} | ||
: s, | ||
) | ||
.map(s => | ||
typeof s === "string" ? s : "link" in s ? `[${s.link}](#${s.link})` : "s" in s ? s.s : "", | ||
) | ||
.join(""); | ||
const func = / => /.test(type); | ||
return [ | ||
!required && func ? "(" : "", | ||
ts, | ||
!required && func ? ")" : "", | ||
!required ? " | undefined" : "", | ||
].join(""); | ||
} | ||
|
||
function split(text: string, splitter: string[]): (string | { s: string })[] { | ||
if (!text.length) return []; | ||
const res: (string | { s: string })[] = []; | ||
let buf = ""; | ||
for (let i = 0; i < text.length; i++) { | ||
buf += text[i]; | ||
const s = splitter.find(s => buf.includes(s)); | ||
if (s) { | ||
res.push(buf.slice(0, -s.length), { s: buf.slice(-s.length) }); | ||
buf = ""; | ||
} | ||
} | ||
if (buf.length) { | ||
res.push(buf); | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.