Merge pull request #5991 from jjaffeux/plugin-sharing-api

FEATURE: adds plugin sharing api
This commit is contained in:
Arpit Jalan 2018-06-13 17:24:03 +05:30 committed by GitHub
commit 4107a02ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 5 deletions

View File

@ -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;

View File

@ -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 plugins
if (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 = [];
}
};

View File

@ -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 doesnt 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"
);
});