-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2802 from AlchemyCMS/preview-window-component
Convert Preview Window into web component
- Loading branch information
Showing
14 changed files
with
190 additions
and
131 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
82 changes: 0 additions & 82 deletions
82
app/assets/javascripts/alchemy/alchemy.preview_window.js.coffee
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
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
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
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,21 @@ | ||
import "alchemy_admin/components/button" | ||
import "alchemy_admin/components/char_counter" | ||
import "alchemy_admin/components/clipboard_button" | ||
import "alchemy_admin/components/datepicker" | ||
import "alchemy_admin/components/dialog_link" | ||
import "alchemy_admin/components/element_editor" | ||
import "alchemy_admin/components/elements_window" | ||
import "alchemy_admin/components/message" | ||
import "alchemy_admin/components/growl" | ||
import "alchemy_admin/components/icon" | ||
import "alchemy_admin/components/ingredient_group" | ||
import "alchemy_admin/components/link_buttons" | ||
import "alchemy_admin/components/node_select" | ||
import "alchemy_admin/components/uploader" | ||
import "alchemy_admin/components/overlay" | ||
import "alchemy_admin/components/page_select" | ||
import "alchemy_admin/components/preview_window" | ||
import "alchemy_admin/components/select" | ||
import "alchemy_admin/components/spinner" | ||
import "alchemy_admin/components/tags_autocomplete" | ||
import "alchemy_admin/components/tinymce" |
121 changes: 121 additions & 0 deletions
121
app/javascript/alchemy_admin/components/preview_window.js
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,121 @@ | ||
const MIN_WIDTH = 240 | ||
|
||
class PreviewWindow extends HTMLIFrameElement { | ||
#afterLoad | ||
#reloadIcon | ||
|
||
constructor() { | ||
super() | ||
this.addEventListener("load", this) | ||
} | ||
|
||
handleEvent(evt) { | ||
if (evt.type === "load") { | ||
this.#stopSpinner() | ||
this.#afterLoad?.call(this, evt) | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
let url = this.url | ||
|
||
this.#attachEvents() | ||
|
||
if (window.localStorage.getItem("alchemy-preview-url")) { | ||
url = window.localStorage.getItem("alchemy-preview-url") | ||
this.previewUrlSelect.value = url | ||
} | ||
|
||
this.refresh(url) | ||
} | ||
|
||
disconnectedCallback() { | ||
key.unbind("alt+r") | ||
} | ||
|
||
postMessage(data) { | ||
this.contentWindow.postMessage(data, "*") | ||
} | ||
|
||
resize(width) { | ||
if (width < MIN_WIDTH) { | ||
width = MIN_WIDTH | ||
} | ||
this.style.width = `${width}px` | ||
} | ||
|
||
refresh(url) { | ||
this.#startSpinner() | ||
|
||
if (url) { | ||
this.src = url | ||
} else { | ||
this.src = this.url | ||
} | ||
|
||
return new Promise((resolve) => { | ||
this.#afterLoad = resolve | ||
}) | ||
} | ||
|
||
#attachEvents() { | ||
this.reloadButton?.addEventListener("click", (evt) => { | ||
evt.preventDefault() | ||
this.refresh() | ||
}) | ||
|
||
key("alt+r", () => this.refresh()) | ||
|
||
// Need to listen with jQuery here because select2 does not emit native events. | ||
$(this.sizeSelect).on("change", (evt) => { | ||
const select = evt.target | ||
const width = select.value | ||
|
||
if (width === "auto") { | ||
this.style.width = null | ||
} else { | ||
this.resize(width) | ||
} | ||
}) | ||
|
||
this.previewUrlSelect?.addEventListener("change", (evt) => { | ||
const url = evt.target.value | ||
window.localStorage.setItem("alchemy-preview-url", url) | ||
this.refresh(url) | ||
}) | ||
} | ||
|
||
#startSpinner() { | ||
this.#reloadIcon = this.reloadButton.innerHTML | ||
this.reloadButton.innerHTML = `<alchemy-spinner size="small"></alchemy-spinner>` | ||
} | ||
|
||
#stopSpinner() { | ||
this.reloadButton.innerHTML = this.#reloadIcon | ||
} | ||
|
||
get url() { | ||
return this.getAttribute("url") | ||
} | ||
|
||
get sizeSelect() { | ||
return document.querySelector("select#preview_size") | ||
} | ||
|
||
get previewUrlSelect() { | ||
return document.querySelector("select#preview_url") | ||
} | ||
|
||
get reloadButton() { | ||
return document.querySelector("#reload_preview_button") | ||
} | ||
} | ||
|
||
customElements.define("alchemy-preview-window", PreviewWindow, { | ||
extends: "iframe" | ||
}) | ||
|
||
export function reloadPreview() { | ||
const previewWindow = document.getElementById("alchemy_preview_window") | ||
previewWindow.refresh() | ||
} |
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.