Skip to content

Commit

Permalink
refactor(EasyArchive): optimize code logic (#1455)
Browse files Browse the repository at this point in the history
* refactor(EasyArchive): optimize code logic
  • Loading branch information
WaitSpringQW authored Jul 22, 2024
1 parent e205204 commit 56082ab
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 165 deletions.
172 changes: 82 additions & 90 deletions dist/EasyArchive/EasyArchive.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/EasyArchive/EasyArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import './EasyArchive.less';
import {
arcLocNotAllowedFooterNotice,
enabledFooterNotice,
inBlacklistFooterNotice,
noArcLocFooterNotice,
notAllowededFooterNotice,
notSupportededFooterNotice,
} from './modules/footerNotice';
import {addLinks} from './modules/addLinks';
import {getSettings} from './modules/settings';
import {ifArcLocNotAllowed} from './modules/ifArcLocNotAllowed';
import {isInBlacklist} from './modules/isInBlacklist';
import {isNotAllowed} from './modules/isNotAllowed';
import {isNotSupported} from './modules/isNotSupported';

(function easyArchive() {
const notSupported = isNotSupported();
if (notSupported) {
notSupportededFooterNotice();
return;
}

const inBlacklist = isInBlacklist();
if (inBlacklist) {
inBlacklistFooterNotice();
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/EasyArchive/modules/addLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {onClickWrap, pipeElement, sectionIdSpanElement} from './react';
import React from 'ext.gadget.React';
import {archive} from './archive';
import {archiveSection} from './archiveSection';
import {getMessage} from './i18n';
import {getSections} from './parse';
import {refresh} from './refreshPage';
import {remove} from './remove';
import {removeSection} from './removeSection';
import {replaceChild} from './replaceChild';
import {toastify} from 'ext.gadget.Toastify';

Expand Down Expand Up @@ -81,7 +81,7 @@ const addLinks = async ({
},
'info'
);
void archive(index, id, arcLoc).then(() => {
void archiveSection(index, id, arcLoc).then(() => {
toastifyInstance.hideToast();
replaceChild(parentElement, <span>{getMessage('Archived')}</span>);
toastifyInstance = toastify(
Expand Down Expand Up @@ -118,7 +118,7 @@ const addLinks = async ({
},
'info'
);
void remove(index, id).then(() => {
void removeSection(index, id).then(() => {
toastifyInstance.hideToast();
replaceChild(parentElement, <span>{getMessage('Deleted')}</span>);
toastifyInstance = toastify(
Expand Down
23 changes: 21 additions & 2 deletions src/EasyArchive/modules/archiveSection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import {api} from './api';
import {checkIfPageExist} from './checkIfPageExist';
import {checkIfSectionExist} from './checkIfSectionExist';
import {getMessage} from './i18n';
import {getSectionContent} from './getSectionContent';
import {removeSection} from './removeSection';

const archiveSection = async (index: string, anchor: string, archiveTo: string) => {
const {wgPageName} = mw.config.get();
const ifSectionExist = await checkIfSectionExist(index, anchor);

if (ifSectionExist !== true) {
return;
}

const content = await getSectionContent(wgPageName, index);

if (content === null) {
return;
}

const archiveSection = async (archiveTo: string, text: string) => {
const pageExist = await checkIfPageExist(archiveTo);
if (!pageExist) {
await api.create(
Expand All @@ -14,13 +30,16 @@ const archiveSection = async (archiveTo: string, text: string) => {
'{{talkarchive}}'
);
}

await api.edit(archiveTo, () => {
return {
appendtext: `\n\n${text}`,
appendtext: `\n\n${content}`,
summary: getMessage('Archive summary'),
minor: true,
};
});

await removeSection(wgPageName, index, getMessage('Archive summary'));
};

export {archiveSection};
15 changes: 0 additions & 15 deletions src/EasyArchive/modules/deleteSection.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/EasyArchive/modules/footerNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const sanitize = (string: string) =>
.replace(/'/g, '&apos;')
.replace(/"/g, '&quot;');

const notSupportededFooterNotice = () => {
const inBlacklistFooterNotice = () => {
footerNotice(
'easy_archive_not_supported_notice',
<span>
Expand Down Expand Up @@ -63,10 +63,9 @@ const enabledFooterNotice = (arcLoc: string) => {
};

export {
footerNotice,
notSupportededFooterNotice,
notAllowededFooterNotice,
noArcLocFooterNotice,
arcLocNotAllowedFooterNotice,
enabledFooterNotice,
inBlacklistFooterNotice,
noArcLocFooterNotice,
notAllowededFooterNotice,
};
25 changes: 25 additions & 0 deletions src/EasyArchive/modules/isInBlacklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const isInBlacklist = () => {
const {wgPageName} = mw.config.get();

const blackListRegexArr = [
/^File:.*$/,
/^MediaWiki:.*$/,
/^Module:.*$/,
/^Category:.*$/,
/^Template:.*$/,
/^Special:.*$/,
/^User:.*\/?.*\.js$/,
/^User:.*\/?.*\.css$/,
/^User:.*\/?.*\.json$/,
];

for (const element of blackListRegexArr) {
if (element.test(wgPageName)) {
return true;
}
}

return false;
};

export {isInBlacklist};
20 changes: 1 addition & 19 deletions src/EasyArchive/modules/isNotSupported.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
const isNotSupported = () => {
const {wgIsMainPage, wgNamespaceNumber, wgPageName, wgCurRevisionId, wgRevisionId} = mw.config.get();

const blackListRegexArr = [
/^File:.*$/,
/^MediaWiki:.*$/,
/^Module:.*$/,
/^Category:.*$/,
/^Template:.*$/,
/^Special:.*$/,
/^User:.*\/?.*\.js$/,
/^User:.*\/?.*\.css$/,
/^User:.*\/?.*\.json$/,
];

for (const element of blackListRegexArr) {
if (element.test(wgPageName)) {
return true;
}
}
const {wgIsMainPage, wgNamespaceNumber, wgCurRevisionId, wgRevisionId} = mw.config.get();

if (wgNamespaceNumber < 0 || wgCurRevisionId - wgRevisionId !== 0) {
return true;
Expand Down
22 changes: 0 additions & 22 deletions src/EasyArchive/modules/remove.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {archiveSection} from './archiveSection';
import {api} from './api';
import {checkIfSectionExist} from './checkIfSectionExist';
import {deleteSection} from './deleteSection';
import {getMessage} from './i18n';
import {getSectionContent} from './getSectionContent';

const archive = async (index: string, anchor: string, archiveTo: string) => {
const removeSection = async (index: string, anchor: string, summary?: string) => {
const {wgPageName} = mw.config.get();
const ifSectionExist = await checkIfSectionExist(index, anchor);

Expand All @@ -17,8 +16,15 @@ const archive = async (index: string, anchor: string, archiveTo: string) => {
if (content === null) {
return;
}
await archiveSection(archiveTo, content);
await deleteSection(wgPageName, index, getMessage('Archive summary'));

await api.edit(wgPageName, () => {
return {
section: index,
text: '',
summary: summary ?? getMessage('Delete summary'),
minor: true,
};
});
};

export {archive};
export {removeSection};

0 comments on commit 56082ab

Please sign in to comment.