Skip to content

Commit

Permalink
🐛 Fix inability to open a recently closed project in the same window
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Nov 7, 2022
1 parent 404846d commit 5656492
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/node_requires/extendGlobals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare global {
function showOpenDialog(options: any): Promise<string | false>;
function showSaveDialog(options: any): Promise<string | false>;
interface Window {
path: string;
id: number;
signals: any;
orders: any;
alertify: any;
Expand Down
41 changes: 23 additions & 18 deletions src/node_requires/resources/projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,20 @@ const readProjectFile = async (proj: string) => {
* This is the method that should be used for opening ct.js projects
* from within UI.
*/
const openProject = async (proj: string): Promise<void> => {
const openProject = async (proj: string): Promise<void | false | Promise<void>> => {
if (!proj) {
const baseMessage = 'An attempt to open a project with an empty path.';
alertify.error(baseMessage + ' See the console for the call stack.');
const err = new Error(baseMessage);
throw err;
}
const proceed = await (new Promise((resolve) => {
(nw.Window as any).getAll((windows: { window: { path: string } }[]) => {
// eslint-disable-next-line camelcase
(nw.Window as any).getAll((windows: NWJS_Helpers.win[]) => {
windows.forEach(win => {
if (win.window.path === proj) {
if ((win.window as Window).path === proj &&
(win.window as Window).id !== window.id
) {
const baseMessage = 'You cannot open the same project multiple times.';
alertify.error(baseMessage);
const err = new Error(baseMessage);
Expand All @@ -218,7 +221,9 @@ const openProject = async (proj: string): Promise<void> => {
resolve(true);
});
}));
if (!proceed) return;
if (!proceed) {
return false;
}
sessionStorage.projname = path.basename(proj);
global.projdir = path.dirname(proj) + path.sep + path.basename(proj, '.ict');

Expand All @@ -234,21 +239,21 @@ const openProject = async (proj: string): Promise<void> => {
const targetStat = await fs.stat(proj);
const voc = window.languageJSON.intro.recovery;
const userResponse = await window.alertify
.okBtn(voc.loadRecovery)
.cancelBtn(voc.loadTarget)
/* {0} — target file date
{1} — target file state (newer/older)
{2} — recovery file date
{3} — recovery file state (newer/older)
*/
.confirm(voc.message
.replace('{0}', targetStat.mtime.toLocaleString())
.replace('{1}', targetStat.mtime < recoveryStat.mtime ? voc.older : voc.newer)
.replace('{2}', recoveryStat.mtime.toLocaleString())
.replace('{3}', recoveryStat.mtime < targetStat.mtime ? voc.older : voc.newer));
.okBtn(voc.loadRecovery)
.cancelBtn(voc.loadTarget)
/* {0} — target file date
{1} — target file state (newer/older)
{2} — recovery file date
{3} — recovery file state (newer/older)
*/
.confirm(voc.message
.replace('{0}', targetStat.mtime.toLocaleString())
.replace('{1}', targetStat.mtime < recoveryStat.mtime ? voc.older : voc.newer)
.replace('{2}', recoveryStat.mtime.toLocaleString())
.replace('{3}', recoveryStat.mtime < targetStat.mtime ? voc.older : voc.newer));
window.alertify
.okBtn(window.languageJSON.common.ok)
.cancelBtn(window.languageJSON.common.cancel);
.okBtn(window.languageJSON.common.ok)
.cancelBtn(window.languageJSON.common.cancel);
if (userResponse.buttonClicked === 'ok') {
return readProjectFile(proj + '.recovery');
}
Expand Down
1 change: 1 addition & 0 deletions src/riotTags/root-tag.tag
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ root-tag(class="{pride: localStorage.prideMode === 'on'}")
writable-folder-prompt(if="{showWritableFolderPrompt}" onsuccess="{onWritableSelected}")
script.
this.projectOpened = false;
window.id = Math.random();
window.signals.on('resetAll', () => {
const glob = require('./data/node_requires/glob');
for (const script of window.currentProject.scripts) {
Expand Down

0 comments on commit 5656492

Please sign in to comment.