Skip to content

Commit 82e7a7d

Browse files
committed
refactor: move toast message to its respective file
1 parent 4e21326 commit 82e7a7d

File tree

3 files changed

+86
-64
lines changed

3 files changed

+86
-64
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function RemoteFunctions(config = {}) {
7777
"redraw",
7878
"onElementSelected", // an item is selected in live preview
7979
"onElementCleanup",
80+
"onNonEditableElementClick", // called when user clicks on a non-editable element
8081
"handleConfigChange",
8182
// below function gets called to render the dropdown when user clicks on the ... menu in the options box,
8283
// the handler should retrun html tor ender the dropdown item.
@@ -1420,7 +1421,6 @@ function RemoteFunctions(config = {}) {
14201421
dismissNodeMoreOptionsBox();
14211422
dismissMoreOptionsDropdown();
14221423
dismissNodeInfoBox();
1423-
dismissToastMessage();
14241424
cleanupPreviousElementState();
14251425

14261426
// this should also be there when users are in highlight mode
@@ -1430,10 +1430,13 @@ function RemoteFunctions(config = {}) {
14301430
return false;
14311431
}
14321432

1433-
// if element is not editable and user clicks on it, then we show a toast notification saying
1434-
// that this element is not editable
1433+
// when user clicks on a non-editable element
14351434
if (!element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
1436-
showToastMessage("notEditable");
1435+
getAllNodeMoreOptionsHandlers().forEach(handler => {
1436+
if (handler.onNonEditableElementClick) {
1437+
handler.onNonEditableElementClick(element);
1438+
}
1439+
});
14371440
}
14381441

14391442
// make sure that the element is actually visible to the user
@@ -2122,68 +2125,9 @@ function RemoteFunctions(config = {}) {
21222125
dismissNodeMoreOptionsBox();
21232126
dismissMoreOptionsDropdown();
21242127
dismissNodeInfoBox();
2125-
dismissToastMessage();
21262128
getAllNodeMoreOptionsHandlers().forEach(handler => (handler.dismiss && handler.dismiss()));
21272129
}
21282130

2129-
let _toastTimeout = null;
2130-
2131-
const TOAST_TYPE_MAPPING = {
2132-
notEditable: strings.toastNotEditable,
2133-
copyFirstTime: strings.toastCopyFirstTime
2134-
};
2135-
2136-
/**
2137-
* this function is to show a toast notification at the bottom center of the screen
2138-
* this toast message is used when user tries to edit a non-editable element
2139-
* @param {String} toastType - toastType determines the message to display in the toast
2140-
* @param {Number} duration - optional duration in milliseconds (default: 3000)
2141-
*/
2142-
function showToastMessage(toastType, duration = 3000) {
2143-
// clear any existing toast & timer, if there are any
2144-
dismissToastMessage();
2145-
2146-
// create a new fresh toast container
2147-
const toast = window.document.createElement('div');
2148-
toast.id = 'phoenix-toast-notification';
2149-
toast.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
2150-
const shadow = toast.attachShadow({ mode: 'open' });
2151-
2152-
const styles = cssStyles.toastMessage;
2153-
2154-
const content = `
2155-
<div class="toast-container">
2156-
<div class="toast-message">${TOAST_TYPE_MAPPING[toastType]}</div>
2157-
</div>
2158-
`;
2159-
2160-
shadow.innerHTML = `<style>${styles}</style>${content}`;
2161-
window.document.body.appendChild(toast);
2162-
2163-
// Auto-dismiss after the given time
2164-
_toastTimeout = setTimeout(() => {
2165-
if (toast && toast.parentNode) {
2166-
toast.remove();
2167-
}
2168-
_toastTimeout = null;
2169-
}, duration);
2170-
}
2171-
2172-
/**
2173-
* this function is to dismiss the toast message
2174-
* and clear its timeout (if any)
2175-
*/
2176-
function dismissToastMessage() {
2177-
const toastMessage = window.document.getElementById('phoenix-toast-notification');
2178-
if (toastMessage) {
2179-
toastMessage.remove();
2180-
}
2181-
if (_toastTimeout) {
2182-
clearTimeout(_toastTimeout);
2183-
}
2184-
_toastTimeout = null;
2185-
}
2186-
21872131
/**
21882132
* Helper function to cleanup previously clicked element highlighting and state
21892133
*/
@@ -2312,7 +2256,6 @@ function RemoteFunctions(config = {}) {
23122256
"applyDOMEdits": applyDOMEdits,
23132257
"updateConfig": updateConfig,
23142258
"dismissUIAndCleanupState": dismissUIAndCleanupState,
2315-
"showToastMessage": showToastMessage,
23162259
"escapeKeyPressInEditor": _escapeKeyPressInEditor,
23172260
"getMode": function() { return config.mode; }
23182261
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2021 - present core.ai
3+
* SPDX-License-Identifier: LicenseRef-Proprietary
4+
*/
5+
/*global GLOBALS, strings, cssStyles, LivePreviewView, customReturns*/
6+
7+
let _toastTimeout = null;
8+
9+
const TOAST_TYPE_MAPPING = {
10+
notEditable: strings.toastNotEditable,
11+
copyFirstTime: strings.toastCopyFirstTime
12+
};
13+
14+
/**
15+
* this function is to show a toast notification at the bottom center of the screen
16+
* this toast message is used when user tries to edit a non-editable element
17+
* @param {String} toastType - toastType determines the message to display in the toast
18+
* @param {Number} duration - optional duration in milliseconds (default: 3000)
19+
*/
20+
function showToastMessage(toastType, duration = 3000) {
21+
// clear any existing toast & timer, if there are any
22+
dismissToastMessage();
23+
24+
// create a new fresh toast container
25+
const toast = window.document.createElement("div");
26+
toast.id = "phoenix-toast-notification";
27+
toast.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
28+
const shadow = toast.attachShadow({ mode: "open" });
29+
30+
const styles = cssStyles.toastMessage;
31+
32+
const content = `
33+
<div class="toast-container">
34+
<div class="toast-message">${TOAST_TYPE_MAPPING[toastType]}</div>
35+
</div>
36+
`;
37+
38+
shadow.innerHTML = `<style>${styles}</style>${content}`;
39+
window.document.body.appendChild(toast);
40+
41+
// Auto-dismiss after the given time
42+
_toastTimeout = setTimeout(() => {
43+
if (toast && toast.parentNode) {
44+
toast.remove();
45+
}
46+
_toastTimeout = null;
47+
}, duration);
48+
}
49+
50+
/**
51+
* this function is to dismiss the toast message
52+
* and clear its timeout (if any)
53+
*/
54+
function dismissToastMessage() {
55+
const toastMessage = window.document.getElementById("phoenix-toast-notification");
56+
if (toastMessage) {
57+
toastMessage.remove();
58+
}
59+
if (_toastTimeout) {
60+
clearTimeout(_toastTimeout);
61+
}
62+
_toastTimeout = null;
63+
}
64+
65+
// this is just a wrapper function which calls the show toast message
66+
// its needed to discard the element param that is passed to it by remote functions
67+
function onNonEditableElementClick(element) {
68+
showToastMessage("notEditable");
69+
}
70+
71+
// Register with LivePreviewView
72+
LivePreviewView.registerNodeMoreOptionsHandler("toast-message", {
73+
onNonEditableElementClick: onNonEditableElementClick,
74+
dismiss: dismissToastMessage
75+
});
76+
77+
customReturns.showToastMessage = showToastMessage;

src/extensionsIntegrated/phoenix-pro/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ define(function (require, exports, module) {
2323
const DragAndDropCode = require("text!./browser-context/dragAndDrop.js");
2424
const HotCornersCode = require("text!./browser-context/hot-corners.js");
2525
const GenericToolsCode = require("text!./browser-context/generic-tools.js");
26+
const ToastMessageCode = require("text!./browser-context/toast-message.js");
2627
const LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol");
2728

2829
function _addRemoteScripts() {
@@ -46,6 +47,7 @@ define(function (require, exports, module) {
4647
LiveDevProtocol.addRemoteFunctionScript("DragAndDrop", DragAndDropCode);
4748
LiveDevProtocol.addRemoteFunctionScript("HotCornersCode", HotCornersCode);
4849
LiveDevProtocol.addRemoteFunctionScript("GenericTools", GenericToolsCode);
50+
LiveDevProtocol.addRemoteFunctionScript("ToastMessageCode", ToastMessageCode);
4951
}
5052
_addRemoteScripts();
5153
});

0 commit comments

Comments
 (0)