Skip to content

Commit

Permalink
refactor(EasyArchive): minify; avoid multiple notices at once
Browse files Browse the repository at this point in the history
  • Loading branch information
WaitSpringQW committed Aug 10, 2024
1 parent 8b94cf2 commit 3269423
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 174 deletions.
115 changes: 40 additions & 75 deletions dist/EasyArchive/EasyArchive.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/EasyArchive/EasyArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import {getSettings} from './modules/getSettings';
return;
}

mw.hook('wikipage.content').add((): void => {
void addLinks(settings);
});
void addLinks(settings);
enabledFooterNotice(arcLoc);
})();
142 changes: 46 additions & 96 deletions src/EasyArchive/modules/addLinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {noticeMessage, onClickWrap, pipeElement, sectionIdSpanElement, spanWrap} from './util/react';
import {type ReactElement} from 'ext.gadget.React';
import {archiveSection} from './archiveSection';
import {getMessage} from './i18n';
import {getSections} from './util/getSection';
Expand Down Expand Up @@ -34,8 +35,12 @@ const addLinks = async ({
headlines[headlines.length] = headline?.id;
}

const arcDelChannel: BroadcastChannel = new BroadcastChannel(wgPageName);
const arcDelMessageChannel: BroadcastChannel = new BroadcastChannel(`${wgPageName}_message`);
const sectionIdSpans: ReactElement[] = [];
let toastifyInstance: ToastifyInstance = {
hideToast: () => {},
};

const messageChannel: BroadcastChannel = new BroadcastChannel(`${wgPageName}_message`);
const refreshChannel: BroadcastChannel = new BroadcastChannel(`${wgPageName}_refresh`);

for (const section of sectionsToArchive) {
Expand Down Expand Up @@ -68,9 +73,6 @@ const addLinks = async ({
continue;
}

let toastifyInstance: ToastifyInstance = {
hideToast: () => {},
};
const sectionIdSpan = sectionIdSpanElement();
const archiveSectionLink = ({
indexNo,
Expand All @@ -89,39 +91,16 @@ const addLinks = async ({
}

replaceChild(parentElement, spanWrap(getMessage('Archiving')));

toastifyInstance = toastify(
{
text:
getMessage('Archiving') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo),
duration: -1,
},
'info'
);
arcDelChannel.postMessage('Archive');
arcDelMessageChannel.postMessage(
messageChannel.postMessage(
getMessage('Archiving') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
);

void archiveSection({index: indexNo, anchor, archiveTo}).then(() => {
toastifyInstance.hideToast();
replaceChild(parentElement, spanWrap(getMessage('Archived')));
arcDelChannel.close();
toastifyInstance = toastify(
{
text: getMessage('Archived'),
duration: 3 * 1000,
},
'success'
);
toastifyInstance = toastify(
{
text: getMessage('Refreshing'),
close: true,
duration: -1,
},
'info'
messageChannel.postMessage(
getMessage('Archived') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
);
messageChannel.close();
refreshChannel.postMessage('Refresh');
refresh();
});
Expand All @@ -137,39 +116,16 @@ const addLinks = async ({
}

replaceChild(parentElement, spanWrap(getMessage('Deleting')));

toastifyInstance = toastify(
{
text:
getMessage('Deleting') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo),
duration: -1,
},
'info'
);
arcDelChannel.postMessage('Delete');
arcDelMessageChannel.postMessage(
messageChannel.postMessage(
getMessage('Deleting') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
);

void removeSection({index: indexNo, anchor}).then(() => {
toastifyInstance.hideToast();
replaceChild(parentElement, spanWrap(getMessage('Deleted')));
arcDelChannel.close();
toastifyInstance = toastify(
{
text: getMessage('Deleted'),
duration: 3 * 1000,
},
'success'
);
toastifyInstance = toastify(
{
text: getMessage('Refreshing'),
close: true,
duration: -1,
},
'info'
messageChannel.postMessage(
getMessage('Deleted') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
);
messageChannel.close();
refreshChannel.postMessage('Refresh');
refresh();
});
Expand All @@ -187,47 +143,41 @@ const addLinks = async ({
const removeLink = removeSectionLink({indexNo: index, anchor: id});
sectionIdSpan.append(removeLink);
}
sectionIdSpans[sectionIdSpans.length] = sectionIdSpan;
editSection.prepend(sectionIdSpan);
}

arcDelChannel.addEventListener('message', () => {
messageChannel.addEventListener('message', (event) => {
for (const sectionIdSpan of sectionIdSpans) {
sectionIdSpan.remove();
});
arcDelMessageChannel.addEventListener('message', (event) => {
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
text: event.data as string,
close: true,
duration: -1,
},
'info'
);
});
refreshChannel.addEventListener('message', () => {
const locationReload = () => {
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
text: getMessage('Refreshing'),
close: true,
duration: -1,
}
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
text: event.data as string,
close: true,
duration: 3 * 1000,
},
'info'
);
});

refreshChannel.addEventListener('message', () => {
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
node: noticeMessage({
onClick: () => {
toastifyInstance.hideToast();
refresh();
},
'info'
);
location.reload();
return false;
};
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
node: noticeMessage({onClick: locationReload}),
close: true,
duration: -1,
},
'info'
);
});
}
}),
close: true,
duration: -1,
},
'info'
);
});
};

export {addLinks};

0 comments on commit 3269423

Please sign in to comment.