@@ -4,6 +4,7 @@ import { GlobalMindmapConfig, LocalMindmapConfig } from '../Mindmap'
44import { Markmap , deriveOptions } from "markmap-view"
55import { Toolbar } from "markmap-toolbar"
66import { IPureNode } from 'markmap-common'
7+ import { h } from 'preact'
78
89const externalIcon = `
910<svg aria-hidden="true" class="external-icon" style="max-width:0.8em;max-height:0.8em; margin-left:0.2em;" viewBox="0 0 512 512">
@@ -29,6 +30,24 @@ const exitIcon = `
2930 d="m256-200-56-56 224-224-224-224 56-56 224 224 224-224 56 56-224 224 224 224-56 56-224-224-224 224Z"/>
3031</svg>`
3132
33+ function fixForeignObjectReplaceElementErrorInSafari ( layout : HTMLElement ) {
34+ layout . querySelectorAll ( "video, audio, iframe" ) . forEach ( el => {
35+ const src = el . getAttribute ( "data-src" ) ?? el . getAttribute ( "src" ) ?? el . querySelector ( "source" ) ?. getAttribute ( "src" )
36+ if ( ! src ) return
37+ const type = el . getAttribute ( "data-type" ) ?? el . tagName . toLowerCase ( )
38+ const place = el . getAttribute ( "data-place" ) ?? "internal"
39+
40+ const a = Object . assign ( document . createElement ( "a" ) , {
41+ href : src ,
42+ target : "_blank" ,
43+ className : `media-fallback ${ place } ` ,
44+ textContent : `${ type } - ${ src } ` ,
45+ } )
46+
47+ el . parentElement ?. replaceChild ( a , el )
48+ a . style . display = "inline-block"
49+ } )
50+ }
3251
3352function renderMermaidInMindmap ( svg : SVGSVGElement ) {
3453 requestAnimationFrame ( async ( ) => {
@@ -164,7 +183,7 @@ function addExternalIcon(svg: SVGSVGElement) {
164183 } )
165184}
166185
167- async function renderMindmap ( mindmap : HTMLElement ) {
186+ async function renderMindmap ( mindmap : HTMLElement , isSafari = false ) {
168187 removeAllChildren ( mindmap )
169188
170189 if ( ! mindmap . dataset [ "mindmap" ] ) {
@@ -186,8 +205,9 @@ async function renderMindmap(mindmap: HTMLElement) {
186205
187206 renderToolbar ( mindmap , mm , option )
188207 renderMermaidInMindmap ( svg )
189- addExternalIcon ( svg )
190208 renderPopoverInMindmap ( )
209+ isSafari && fixForeignObjectReplaceElementErrorInSafari ( mindmap )
210+ addExternalIcon ( svg )
191211
192212 return ( ) => {
193213 mindmap . innerHTML = ""
@@ -220,7 +240,7 @@ document.addEventListener("nav", async () => {
220240 const mindmapContainers = document . getElementsByClassName ( "mindmap-container" )
221241 for ( const container of mindmapContainers ) {
222242 isSafari && container . classList . add ( "is-safari" )
223- localMindmapCleanups . push ( await renderMindmap ( container as HTMLElement ) )
243+ localMindmapCleanups . push ( await renderMindmap ( container as HTMLElement , isSafari ) )
224244 }
225245 }
226246
@@ -247,7 +267,7 @@ document.addEventListener("nav", async () => {
247267 registerEscapeHandler ( container , hideGlobalMindmap )
248268 if ( mindmapContainer ) {
249269 isSafari && mindmapContainer . classList . add ( "is-safari" )
250- globalMindmapCleanups . push ( await renderMindmap ( mindmapContainer ) )
270+ globalMindmapCleanups . push ( await renderMindmap ( mindmapContainer , isSafari ) )
251271 }
252272 }
253273 }
0 commit comments