A platform for community discussion. Free, open, simple.
FEATURE: adds plugin sharing api
Joffrey JAFFEUXeddcb07f0340
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6index 3222c2ddd26..d44ebbc26bc 100644--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6@@ -26,9 +26,10 @@ import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker';import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar';import { disableNameSuppression } from 'discourse/widgets/poster-name';import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from 'discourse/controllers/topic';+import Sharing from 'discourse/lib/sharing';// If you add any methods to the API ensure you bump up this number-const PLUGIN_API_VERSION = '0.8.22';+const PLUGIN_API_VERSION = '0.8.23';class PluginApi {constructor(version, container) {@@ -710,6 +711,21 @@ class PluginApi {addGTMPageChangedCallback(fn) {addGTMPageChangedCallback(fn);}++ /**+ *+ * Registers a function that can add a new sharing source+ *+ * Example:+ *+ * // read /discourse/lib/sharing.js.es6 for options+ * addSharingSource(options)+ *+ */+ addSharingSource(options) {+ Sharing.addSharingId(options.id);+ Sharing.addSource(options);+ }}let _pluginv01;diff --git a/app/assets/javascripts/discourse/lib/sharing.js.es6 b/app/assets/javascripts/discourse/lib/sharing.js.es6index ef485ed24fa..cdd6ec506e6 100644--- a/app/assets/javascripts/discourse/lib/sharing.js.es6+++ b/app/assets/javascripts/discourse/lib/sharing.js.es6@@ -26,20 +26,37 @@```**/-var _sources = {};+let _sources = {};+let _customSharingIds = [];export default {+ // allows to by pass site settings and add a sharing id through plugin api+ // useful for theme components for example when only few users want to add+ // sharing to a specific third party+ addSharingId(id) {+ _customSharingIds.push(id);+ },+addSource(source) {// backwards compatibility for pluginsif (source.faIcon) {- source.icon = source.faIcon.replace('fa-', '');+ source.icon = source.faIcon.replace("fa-", "");delete source.faIcon;}_sources[source.id] = source;},- activeSources(linksSetting) {- return linksSetting.split('|').map(s => _sources[s]).compact();+ activeSources(linksSetting = "") {+ return linksSetting+ .split("|")+ .concat(_customSharingIds)+ .map(s => _sources[s])+ .compact();+ },++ _reset() {+ _sources = {};+ _customSharingIds = [];}};diff --git a/test/javascripts/lib/sharing-test.js.es6 b/test/javascripts/lib/sharing-test.js.es6new file mode 100644index 00000000000..2a6f249b780--- /dev/null+++ b/test/javascripts/lib/sharing-test.js.es6@@ -0,0 +1,42 @@+import Sharing from "discourse/lib/sharing";++QUnit.module("lib:sharing", {+ beforeEach() {+ Sharing._reset();+ }+});++QUnit.test("addSource", assert => {+ const sharingSettings = "facebook|twitter";++ assert.blank(Sharing.activeSources(sharingSettings));++ Sharing.addSource({+ id: "facebook"+ });++ assert.equal(Sharing.activeSources(sharingSettings).length, 1);+});++QUnit.test("addSharingId", assert => {+ const sharingSettings = "";++ assert.blank(Sharing.activeSources(sharingSettings));++ Sharing.addSource({+ id: "new-source"+ });++ assert.blank(+ Sharing.activeSources(sharingSettings),+ "it doesn’t activate a source not in settings"+ );++ Sharing.addSharingId("new-source");++ assert.equal(+ Sharing.activeSources(sharingSettings).length,+ 1,+ "it adds sharing id to existing sharing settings"+ );+});