feat: Add a separate browser and toggle control for each link #27

Merged
glen merged 2 commits from toggle_viewer into main 2023-09-19 04:40:24 +00:00
Showing only changes of commit 9fdf43a165 - Show all commits

View File

@ -2,16 +2,22 @@ import https://code.jquery.com/jquery-3.7.1.js
X3D from https://create3000.github.io/code/x_ite/latest/x_ite.mjs X3D from https://create3000.github.io/code/x_ite/latest/x_ite.mjs
{convert} from ./deps/vrml1to97/index.js {convert} from ./deps/vrml1to97/index.js
certainlyHandled := '.x3d .gltf .glb .obj .stl .ply'.split ' ' knownExtensions := /[.](?:wrl|x3d|gltf|glb|obj|stl|ply)$/
// The next line will need to change when we handle pages with more than certainlyHandled :=
// one link, since we will potentially need a canvas for each one. knownExtensions.source.slice(0, -2).split('wrl|')[1].split '|'
canvas := X3D.createBrowser()
// Fix up the css so the browser is a nice size and we can see the world info function makeBrowser(url: string)
$(canvas).css width: '150px', height: '150px', overflow: 'visible' canvas := X3D.createBrowser()
x_ite_shadow := canvas.shadowRoot
if x_ite_shadow // Fix up css: smaller browser, but we can see the world info
x_ite_style := x_ite_shadow.querySelector('link') $(canvas).css
width: '150px'
height: '150px'
overflow: 'visible'
'vertical-align': 'top'
x_ite_shadow := canvas.shadowRoot
if x_ite_shadow
x_ite_style := x_ite_shadow.querySelector 'link'
if x_ite_style if x_ite_style
$(x_ite_style).after '<style type="text/css"> $(x_ite_style).after '<style type="text/css">
.x_ite-private-world-info-overlay { .x_ite-private-world-info-overlay {
@ -23,15 +29,13 @@ if x_ite_shadow
if x_ite_browser if x_ite_browser
$(x_ite_browser).css overflow: 'visible' $(x_ite_browser).css overflow: 'visible'
// Now get the browser and load the requested VRML, converting if need be // Now get the browser and load the requested VRML, converting if need be
browser := X3D.getBrowser canvas browser := X3D.getBrowser canvas
browser.setBrowserOption 'StraightenHorizon', false browser.setBrowserOption 'StraightenHorizon', false
site := $('a[href^="http"]')
url := site.attr('href') ?? ''
if certainlyHandled.some((ext) => url.includes ext) if certainlyHandled.some((ext) => url.includes ext)
canvas.setAttribute 'src', url canvas.setAttribute 'src', url
else if url.includes '.wrl' else if url.includes '.wrl'
// Need to obtain the text and check what level it is // Need to obtain the text and check what level it is
response := await fetch url response := await fetch url
text .= await response.text() text .= await response.text()
@ -40,5 +44,28 @@ else if url.includes '.wrl'
browser.baseURL = url browser.baseURL = url
scene := await browser.createX3DFromString text scene := await browser.createX3DFromString text
browser.replaceWorld scene browser.replaceWorld scene
canvas
site.after(canvas) // Put eye icons after all of the eligible links
links := $('a').filter -> !!@.getAttribute('href')?.match knownExtensions
links.after ->
newSpan := $('<span>👁</span>')
newSpan.hover
(-> $(@).css 'background-color', 'lightblue'),
(-> $(@).css 'background-color', 'inherit')
newSpan.on 'click', @,
(e) =>
eye := e.target
state .= eye.getAttribute 'data'
unless state
state = 'off'
url := e.data.getAttribute('href') ?? ''
$(eye).after await makeBrowser url
if state is 'off'
eye.setAttribute 'data', 'on'
$(eye).css 'text-decoration', 'line-through 3px'
$(eye.nextSibling as Element).show()
else
eye.setAttribute 'data', 'off'
$(eye).css 'text-decoration', 'none'
$(eye.nextSibling as Element).hide()