Skip to content

Commit

Permalink
native js workaround refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyTakinTeller committed Aug 30, 2024
1 parent 164706a commit aa439a2
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 114 deletions.
2 changes: 1 addition & 1 deletion export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ advanced_options=false
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
include_filter="*.js"
exclude_filter=""
export_path=".export/web/index.html"
encryption_include_filters=""
Expand Down
2 changes: 1 addition & 1 deletion global/const/game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const VERSION_MINOR: String = "week 18.2"
const PARAMS: Dictionary = PARAMS_PROD #PARAMS_PROD #PARAMS_DEBUG

## BEFORE EXPORTING TO WEB (that uses iframe like e.g. itch.io), SET TO true
const WEB_EXPORT_WORKAROUNDS: bool = false
const WEB_EXPORT_WORKAROUNDS: bool = true

const PARAMS_DEBUG: Dictionary = {
"CLIPBOARD_WEB_WORKAROUND": WEB_EXPORT_WORKAROUNDS,
Expand Down
9 changes: 9 additions & 0 deletions global/utils/file_system_utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ const RESOURCES_PATH: String = "res://resources/"
const IMAGE_RESOURCES_PATH: String = "res://assets/image/"


static func read_text_from(path: String) -> String:
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
if file == null:
return ""
var content: String = file.get_as_text()
file.close()
return content


static func get_files_at(path: String) -> PackedStringArray:
var files: PackedStringArray = DirAccess.get_files_at(path)
if Game.PARAMS["debug_logs"]:
Expand Down
108 changes: 108 additions & 0 deletions native/js/text_modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
async function customPrompt() {
return new Promise((resolve) => {

// Define a global variable to store the textarea value
var globalTextAreaResult;

// Create a semi-transparent background overlay
var overlay = document.createElement("div");
overlay.style.position = "fixed";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.width = "100%";
overlay.style.height = "100%";
overlay.style.overflow = "auto";
overlay.style.backgroundColor = "rgba(0,0,0,0.5)";
overlay.style.zIndex = "9999"; // Ensure it's on top of other content

// Create the modal container
var modal = document.createElement("div");
modal.style.position = "fixed";
modal.style.top = "50%";
modal.style.left = "50%";
modal.style.transform = "translate(-50%, -50%)";
modal.style.backgroundColor = "#333"; // Set modal background to gray
modal.style.color = "white"; // Set text color to white for contrast
modal.style.padding = "20px";
modal.style.borderRadius = "8px";
modal.style.boxShadow = "0 2px 10px rgba(0, 0, 0, 0.1)";
modal.style.zIndex = "10000"; // Above the overlay
modal.style.minWidth = "50px"; // Set minimum width for the modal
modal.style.maxWidth = "80%"; // Limit modal width to a percentage of the viewport
modal.style.maxHeight = "80%"; // Limit modal height to a percentage of the viewport
modal.style.overflowY = "auto"; // Scroll if content is too long
modal.style.overflow = "auto";

// Create the title element
var modalTitle = document.createElement("h2");
modalTitle.textContent = "{atitle}";
modalTitle.style.marginTop = "0"; // Remove top margin
modalTitle.style.marginBottom = "5px"; // Add space below the title
modalTitle.style.color = "white"; // White text color for the title

// Create the subtitle element
var modalSubtitle = document.createElement("h3");
modalSubtitle.textContent = "{subtitle}";
modalSubtitle.style.marginTop = "0"; // Remove top margin
modalSubtitle.style.marginBottom = "15px"; // Add space below the subtitle
modalSubtitle.style.color = "lightgray"; // Light gray text color for the subtitle

// Create the textarea element
var textArea = document.createElement("textarea");
textArea.value = "{input}"
textArea.style.width = "100%"; // Make textarea full width
textArea.style.height = "100%"; // Make textarea full width
textArea.style.minHeight = "50px"; // Set a fixed height for the textarea
textArea.style.backgroundColor = "#444"; // Darker gray background for textarea
textArea.style.color = "white"; // White text color
textArea.style.border = "2px solid #666"; // Add a default border color
textArea.style.borderRadius = "4px"; // Rounded corners
textArea.style.resize = "none"; // Prevent resizing
textArea.style.boxShadow = "0 0 5px rgba(255, 255, 255, 0.3)"; // Subtle shadow for unfocused state

// Add hover effect for the textarea
textArea.addEventListener('mouseover', function() {
textArea.style.borderColor = "#888"; // Change border color on hover
});

textArea.addEventListener('mouseout', function() {
textArea.style.borderColor = "#666"; // Revert border color when not hovering
});

// Append the title, subtitle, and textarea to the modal
modal.appendChild(modalTitle);
modal.appendChild(modalSubtitle);
modal.appendChild(textArea);

// Create a close button
var closeButton = document.createElement("button");
closeButton.textContent = "{button}";
closeButton.style.marginTop = "20px";
closeButton.style.display = "block";
closeButton.style.marginLeft = "auto";
closeButton.style.marginRight = "auto";
closeButton.style.backgroundColor = "#555"; // Darker gray background for button
closeButton.style.color = "white"; // White text color for button
closeButton.style.border = "none";
closeButton.style.padding = "10px 20px";
closeButton.style.borderRadius = "5px";
closeButton.onclick = function() {
// Set the global variable to the value of the textarea
globalTextAreaResult = textArea.value;
window.globalTextAreaResult = textArea.value;
resolve(textArea.value); // Resolve the promise with the textarea value
// Remove modal and overlay from the document
document.body.removeChild(modal);
document.body.removeChild(overlay);
};

// Append the close button to the modal
modal.appendChild(closeButton);

// Append the modal and overlay to the body
document.body.appendChild(overlay);
document.body.appendChild(modal);

});
}
customPrompt();
120 changes: 8 additions & 112 deletions scenes/ui/save_file_modal/save_file_modal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class_name SaveFileModal extends Control

enum Mode { IMPORT, EXPORT }

var text_modal_js_path: String = "res://native/js/text_modal.js"
var text_modal_js: String = FileSystemUtils.read_text_from(text_modal_js_path)

@onready var title: Label = %Title
@onready var text_area: TextEdit = %TextArea
@onready var close_button: Button = %CloseButton
Expand Down Expand Up @@ -29,6 +32,9 @@ func _initialize() -> void:
visible = false
text_area.clear()

if Game.PARAMS["debug_logs"]:
prints("NATIVE JS files", FileSystemUtils.get_files_at("res://native/js/"))


func _set_import_ui_labels() -> void:
title.text = Locale.get_ui_label("import_title")
Expand Down Expand Up @@ -193,116 +199,6 @@ func _await_native_js_popup_command(
func _get_native_js_popup_command(
input: String, atitle: String, subtitle: String, button: String
) -> String:
return (
'''
async function customPrompt() {
return new Promise((resolve) => {
// Define a global variable to store the textarea value
var globalTextAreaResult;
// Create a semi-transparent background overlay
var overlay = document.createElement("div");
overlay.style.position = "fixed";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.width = "100%";
overlay.style.height = "100%";
overlay.style.overflow = "auto";
overlay.style.backgroundColor = "rgba(0,0,0,0.5)";
overlay.style.zIndex = "9999"; // Ensure it's on top of other content
// Create the modal container
var modal = document.createElement("div");
modal.style.position = "fixed";
modal.style.top = "50%";
modal.style.left = "50%";
modal.style.transform = "translate(-50%, -50%)";
modal.style.backgroundColor = "#333"; // Set modal background to gray
modal.style.color = "white"; // Set text color to white for contrast
modal.style.padding = "20px";
modal.style.borderRadius = "8px";
modal.style.boxShadow = "0 2px 10px rgba(0, 0, 0, 0.1)";
modal.style.zIndex = "10000"; // Above the overlay
modal.style.minWidth = "50px"; // Set minimum width for the modal
modal.style.maxWidth = "80%"; // Limit modal width to a percentage of the viewport
modal.style.maxHeight = "80%"; // Limit modal height to a percentage of the viewport
modal.style.overflowY = "auto"; // Scroll if content is too long
modal.style.overflow = "auto";
// Create the title element
var modalTitle = document.createElement("h2");
modalTitle.textContent = "{atitle}";
modalTitle.style.marginTop = "0"; // Remove top margin
modalTitle.style.marginBottom = "5px"; // Add space below the title
modalTitle.style.color = "white"; // White text color for the title
// Create the subtitle element
var modalSubtitle = document.createElement("h3");
modalSubtitle.textContent = "{subtitle}";
modalSubtitle.style.marginTop = "0"; // Remove top margin
modalSubtitle.style.marginBottom = "15px"; // Add space below the subtitle
modalSubtitle.style.color = "lightgray"; // Light gray text color for the subtitle
// Create the textarea element
var textArea = document.createElement("textarea");
textArea.value = "{input}"
textArea.style.width = "100%"; // Make textarea full width
textArea.style.height = "100%"; // Make textarea full width
textArea.style.minHeight = "50px"; // Set a fixed height for the textarea
textArea.style.backgroundColor = "#444"; // Darker gray background for textarea
textArea.style.color = "white"; // White text color
textArea.style.border = "2px solid #666"; // Add a default border color
textArea.style.borderRadius = "4px"; // Rounded corners
textArea.style.resize = "none"; // Prevent resizing
textArea.style.boxShadow = "0 0 5px rgba(255, 255, 255, 0.3)"; // Subtle shadow for unfocused state
// Add hover effect for the textarea
textArea.addEventListener('mouseover', function() {
textArea.style.borderColor = "#888"; // Change border color on hover
});
textArea.addEventListener('mouseout', function() {
textArea.style.borderColor = "#666"; // Revert border color when not hovering
});
// Append the title, subtitle, and textarea to the modal
modal.appendChild(modalTitle);
modal.appendChild(modalSubtitle);
modal.appendChild(textArea);
// Create a close button
var closeButton = document.createElement("button");
closeButton.textContent = "{button}";
closeButton.style.marginTop = "20px";
closeButton.style.display = "block";
closeButton.style.marginLeft = "auto";
closeButton.style.marginRight = "auto";
closeButton.style.backgroundColor = "#555"; // Darker gray background for button
closeButton.style.color = "white"; // White text color for button
closeButton.style.border = "none";
closeButton.style.padding = "10px 20px";
closeButton.style.borderRadius = "5px";
closeButton.onclick = function() {
// Set the global variable to the value of the textarea
globalTextAreaResult = textArea.value;
window.globalTextAreaResult = textArea.value;
resolve(textArea.value); // Resolve the promise with the textarea value
// Remove modal and overlay from the document
document.body.removeChild(modal);
document.body.removeChild(overlay);
};
// Append the close button to the modal
modal.appendChild(closeButton);
// Append the modal and overlay to the body
document.body.appendChild(overlay);
document.body.appendChild(modal);
});
}
customPrompt();
'''
. format({"input": input, "atitle": atitle, "subtitle": subtitle, "button": button})
return text_modal_js.format(
{"input": input, "atitle": atitle, "subtitle": subtitle, "button": button}
)

0 comments on commit aa439a2

Please sign in to comment.