diff --git a/_locales/en/messages.json b/_locales/en/messages.json index d735ab85..29f6105e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1365,6 +1365,10 @@ "message": "Sections", "description": "Header for the table of contents block listing style section names in the left panel of the classic editor" }, + "linking": { + "message": "Linking", + "description": "Header for the section to link the style with other services." + }, "shortcuts": { "message": "Shortcuts", "description": "Go to shortcut configuration" diff --git a/background/style-manager.js b/background/style-manager.js index 781ccef5..8c9c6f3c 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -354,14 +354,23 @@ const styleMan = (() => { if (port.name !== 'link-style-usw') { return; } - port.onMessage.addListener(async style => { + port.onMessage.addListener(async incData => { + const {data: style, reason} = incData; if (!style.id) { return; } - const resultToken = await tokenMan.getToken('userstylesworld', true, style.id); - style._uswToken = resultToken; - await saveStyle(style); - broadcastStyleUpdated(style, 'updateLinking'); + switch (reason) { + case 'link': + style._uswToken = await tokenMan.getToken('userstylesworld', true, style.id); + handleSave(await saveStyle(style), 'success-linking', true); + break; + + case 'revoke': + await tokenMan.revokeToken('userstylesworld', style.id); + style._uswToken = ''; + handleSave(await saveStyle(style), 'success-revoke', true); + break; + } }); } diff --git a/background/token-manager.js b/background/token-manager.js index 3820ac1d..40c8c0ea 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -94,9 +94,9 @@ const tokenMan = (() => { return authUser(name, k, interactive); }, - async revokeToken(name) { + async revokeToken(name, styleId) { const provider = AUTH[name]; - const k = tokenMan.buildKeys(name); + const k = tokenMan.buildKeys(name, styleId); if (provider.revoke) { try { const token = await chromeLocal.getValue(k.TOKEN); diff --git a/edit.html b/edit.html index 377db734..ad1fffd7 100644 --- a/edit.html +++ b/edit.html @@ -59,6 +59,7 @@ + @@ -391,13 +392,20 @@ + + + + Link style + + + This style has been linked. + Revoke linking + + - - Link style - : diff --git a/edit/edit.js b/edit/edit.js index aaedde61..602fa242 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -11,6 +11,7 @@ /* global linterMan */ /* global prefs */ /* global t */// localization.js +/* global updateUI, linkToUSW revokeLinking */// usw-linking.js 'use strict'; //#region init @@ -18,6 +19,7 @@ baseInit.ready.then(async () => { await waitForSheet(); (editor.isUsercss ? SourceEditor : SectionsEditor)(); + updateUI(); await editor.ready; editor.ready = true; editor.dirty.onChange(editor.updateDirty); @@ -42,8 +44,8 @@ baseInit.ready.then(async () => { require(['/edit/linter-dialogs'], () => linterMan.showLintConfig()); $('#lint-help').onclick = () => require(['/edit/linter-dialogs'], () => linterMan.showLintHelp()); - $('#debug-button').onclick = () => - require(['/edit/usw-debug'], () => linkToUSW()); /* global linkToUSW */ + $('#link-style').onclick = () => linkToUSW(); + $('#revoke-style').onclick = () => revokeLinking(); require([ '/edit/autocomplete', '/edit/global-search', @@ -57,10 +59,14 @@ msg.onExtension(request => { if (editor.style.id === style.id) { if (!['editPreview', 'editPreviewEnd', 'editSave', 'config'].includes(request.reason)) { Promise.resolve(request.codeIsUpdated === false ? style : API.styles.get(style.id)) - .then(newStyle => editor.replaceStyle(newStyle, request.codeIsUpdated)); - } - if (request.reason === 'updateLinking') { - console.log(editor.style._uswToken); + .then(newStyle => { + editor.replaceStyle(newStyle, request.codeIsUpdated); + + if (['success-linking', 'success-revoke'].includes(request.reason)) { + updateUI(newStyle); + console.log(editor.style._uswToken); + } + }); } } break; diff --git a/edit/usw-debug.js b/edit/usw-debug.js deleted file mode 100644 index 5164d033..00000000 --- a/edit/usw-debug.js +++ /dev/null @@ -1,16 +0,0 @@ -/* global editor */ - -'use strict'; - -let uswPort; - -/* exported linkToUSW */ -function linkToUSW() { - if (!uswPort) { - uswPort = chrome.runtime.connect({name: 'link-style-usw'}); - uswPort.onDisconnect.addListener(err => { - throw err; - }); - } - uswPort.postMessage(editor.style); -} diff --git a/edit/usw-linking.js b/edit/usw-linking.js new file mode 100644 index 00000000..cd72347a --- /dev/null +++ b/edit/usw-linking.js @@ -0,0 +1,42 @@ +/* global $ */// dom.js +/* global editor */ + +'use strict'; + +let uswPort; + +function connectToPort() { + if (!uswPort) { + uswPort = chrome.runtime.connect({name: 'link-style-usw'}); + uswPort.onDisconnect.addListener(err => { + throw err; + }); + } +} + +/* exported linkToUSW */ +function linkToUSW() { + connectToPort(); + + uswPort.postMessage({reason: 'link', data: editor.style}); +} + +/* exported revokeLinking */ +function revokeLinking() { + connectToPort(); + + uswPort.postMessage({reason: 'revoke', data: editor.style}); +} + + +/* exported updateUI */ +function updateUI(useStyle) { + const style = useStyle || editor.style; + if (style._uswToken) { + $('#after-linking').style = ''; + $('#pre-linking').style = 'display: none;'; + } else { + $('#after-linking').style = 'display: none;'; + $('#pre-linking').style = ''; + } +}
This style has been linked.