Skip to content

Commit

Permalink
extracted world restoring when printing
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-demos-stefan-untitled-board-game-ubg-cards-exporter.js,AUTO-COMMIT-src-client-lively.js,AUTO-COMMIT-src-components-widgets-lively-markdown.js,AUTO-COMMIT-src-components-widgets-lively-presentation.js,
  • Loading branch information
onsetsu committed Dec 17, 2024
1 parent 0a9328a commit 016abee
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 70 deletions.
18 changes: 4 additions & 14 deletions demos/stefan/untitled-board-game/ubg-cards-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ const POKER_CARD_SIZE_MM = POKER_CARD_SIZE_INCHES.scaleBy(25.4);
export default class CardExporter {

/*MD ## Helper MD*/
static async printWithSavedWorld(fn) {
const oldBody = window.oldBody = Array.from(document.body.childNodes)
const bodyCSS = document.body.style.cssText
const title = document.title

try {
static async print(fn) {
lively.printWithSavedWorld(async () => {
await Promise.race([fn(), lively.sleep(5 * 60 * 1000)])

document.querySelectorAll('lively-notification-list').forEach(list => list.remove())
Expand All @@ -36,18 +32,12 @@ export default class CardExporter {
await lively.sleep(1000)
window.print()
await lively.sleep(1000)
return
} finally {
document.body.innerHTML = ""
document.body.style = bodyCSS
document.body.append(...oldBody)
document.title = title
}
})
}

/*MD ## Layout & Rendering MD*/
static async execute(cardsToPrint, ubgCards, skipCardBack) {
this.printWithSavedWorld(async () => {
this.print(async () => {
const body = document.body
body.innerHTML = ''
// body.style = ""
Expand Down
35 changes: 33 additions & 2 deletions src/client/lively.js
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ export default class Lively {



/*MD ### Focus MD*/
/*MD ## Focus MD*/

static isGlobalKeyboardFocusElement(element) {
return element === document.body || element && element.id == "copy-hack-element" || element && element.tagName == "LIVELY-CONTAINER" && element.shadowRoot && !element.shadowRoot.activeElement;
Expand Down Expand Up @@ -2253,6 +2253,7 @@ export default class Lively {
//console.log("scroll back " + scrollTop + " " + scrollLeft )
}


static ensureID(element) {
var id = element.getAttribute("data-lively-id");
if (!id) {
Expand All @@ -2263,7 +2264,7 @@ export default class Lively {
}



/*MD ## DOM MD*/
static deeepElementByID(id) {
if (!id) return;
for(var ea of lively.allElements(true)) {
Expand Down Expand Up @@ -2618,7 +2619,37 @@ export default class Lively {
return this.allParents(element, undefined, true).includes(otherElement);
}

/*MD ## Print Elements MD*/
static async printWithSavedWorld(printFn) {
const bodyCSS = document.body.style.cssText
const oldBody = window.oldBody = Array.from(document.body.childNodes)
const title = document.title

try {
return await printFn()
} finally {
document.body.style = bodyCSS
document.body.replaceChildren(...oldBody)
document.title = title
}
}

static memorizeElementDOMPosition(element) {
const originalParent = element.parentNode;
const originalIndex = Array.from(originalParent.children).indexOf(element);
return { originalParent, originalIndex }
}

static restoreElementDOMPosition(element, memorizedState) {
const { originalParent, originalIndex } = memorizedState
if (originalParent.children.length > originalIndex) {
originalParent.insertBefore(element, originalParent.children[originalIndex]);
} else {
originalParent.appendChild(element);
}
}

/*MD ## --- MD*/
static showHalo(element) {
window.that = element;
HaloService.showHalos(element);
Expand Down
25 changes: 5 additions & 20 deletions src/components/widgets/lively-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,34 +350,19 @@ export default class LivelyMarkdown extends Morph {
}

async print() {
const bodyCSS = document.body.style.cssText
const oldBody = window.oldBody = Array.from(document.body.childNodes)
const title = document.title

try {
lively.printWithSavedWorld(async () => {
const livelyWindow = lively.findParent(this, e => e.localName === 'lively-window', { deep: true })
document.title = livelyWindow?.title || `markdown document`

const originalParent = this.parentNode;
const originalIndex = Array.from(originalParent.children).indexOf(this);
const originalPos = lively.memorizeElementDOMPosition(this);

document.body.replaceChildren(this)

// await lively.sleep(1000)
window.print()

if (originalParent.children.length > originalIndex) {
originalParent.insertBefore(this, originalParent.children[originalIndex]);
} else {
originalParent.appendChild(this);
}

return
} finally {
document.body.style = bodyCSS
document.body.replaceChildren(...oldBody)
document.title = title
}
lively.restoreElementDOMPosition(this, originalPos)
})
}

onPresentationButton() {
Expand Down
63 changes: 29 additions & 34 deletions src/components/widgets/lively-presentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,47 +208,42 @@ export default class LivelyPresentation extends Morph {
var currentSlideNumber = this.currentSlideNumber()
this.showAllSlides()

window.oldBody = Array.from(document.body.childNodes)
var bodyCSS = document.body.style.cssText

try {
// give lively-scripts id's so, we can rescue the content...
var printId = 0
var originals = new Map()
this.querySelectorAll("lively-script").forEach(ea => {
var id = "" + printId++
originals.set(id, ea)
ea.setAttribute("print-id", id)
})
await lively.printWithSavedWorld(async () => {
// give lively-scripts id's so, we can rescue the content...
var printId = 0
var originals = new Map()
this.querySelectorAll("lively-script").forEach(ea => {
var id = "" + printId++
originals.set(id, ea)
ea.setAttribute("print-id", id)
})

document.body.innerHTML = this.innerHTML
.replace(/<lively-script/g,"<lively-no-script")
.replace(/<\/lively-script/g,"</lively-no-script")
Array.from(document.body.querySelectorAll("lively-no-script")).forEach(ea => {
var id = ea.getAttribute("print-id")
var original = originals.get(id, ea)
if (original) {
var replacement = <div>hello</div>
replacement.attachShadow({mode: 'open'})
replacement.shadowRoot.innerHTML = original.get("#result").innerHTML
ea.parentElement.replaceChild(replacement, ea)
}
})
document.body.innerHTML = this.innerHTML
.replace(/<lively-script/g,"<lively-no-script")
.replace(/<\/lively-script/g,"</lively-no-script")

Array.from(document.body.querySelectorAll("lively-no-script")).forEach(ea => {
var id = ea.getAttribute("print-id")
var original = originals.get(id, ea)
if (original) {
var replacement = <div>hello</div>
replacement.attachShadow({mode: 'open'})
replacement.shadowRoot.innerHTML = original.get("#result").innerHTML
ea.parentElement.replaceChild(replacement, ea)
}
})

document.body.style = ""
document.body.style = ""

await lively.sleep(3000)
await lively.sleep(3000)

window.print()
window.print()

// await lively.sleep(1000)
// await lively.confirm("finished printing?")
// await lively.sleep(1000)
// await lively.confirm("finished printing?")
})
} finally {
// I'll be back
document.body.innerHTML = "" // tabula raza
document.body.style = bodyCSS
window.oldBody.forEach(ea => document.body.appendChild(ea))
this.gotoSlideAt(currentSlideNumber)
}
}
Expand Down

0 comments on commit 016abee

Please sign in to comment.