mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 20:24:05 -06:00
Merge pull request #5991 from jjaffeux/plugin-sharing-api
FEATURE: adds plugin sharing api
This commit is contained in:
commit
4107a02ae8
@ -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;
|
||||
|
@ -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 = [];
|
||||
}
|
||||
};
|
||||
|
42
test/javascripts/lib/sharing-test.js.es6
Normal file
42
test/javascripts/lib/sharing-test.js.es6
Normal 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 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"
|
||||
);
|
||||
});
|
Loading…
Reference in New Issue
Block a user