@peerboard/core
Advanced tools
Comparing version
@@ -14,6 +14,10 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var PEERBOARD_EMBED_SDK_URL = 'https://static.peerboard.org/embed/embed.js'; | ||
exports.createForum = exports.loadSdk = void 0; | ||
var PEERBOARD_EMBED_SDK_URL = 'https://static.peerboard.com/embed/embed.js'; | ||
var trimLeftSlash = function (str) { | ||
return str.startsWith('/') ? str.substr(1) : str; | ||
}; | ||
var forumSDK = null; | ||
var loadingSDK = null; | ||
var loadSdk = function (embedSDKURL) { | ||
exports.loadSdk = function (embedSDKURL) { | ||
if (forumSDK !== null) { | ||
@@ -42,19 +46,29 @@ return Promise.resolve(); | ||
}; | ||
exports.default = { | ||
loadSdk: loadSdk, | ||
createForum: function (forumID, container, options) { | ||
var opts = __assign({ resize: true, hideMenu: true, scrollTarget: 'top', baseURL: options.baseURL || "https://peerboard." + window.document.location.hostname }, options); | ||
return loadSdk(options.sdkURL || PEERBOARD_EMBED_SDK_URL).then(function () { | ||
if (!forumSDK) { | ||
throw new Error("Forum should be loaded at the moment."); | ||
} | ||
forumSDK.createForum(forumID, container, opts); | ||
}).catch(function (err) { | ||
console.error("Error creating forum: ", err); | ||
if (options.onFail) { | ||
options.onFail(); | ||
} | ||
}); | ||
var defaultOptions = { | ||
resize: true, | ||
hideMenu: true, | ||
baseURL: "https://peerboard." + window.document.location.hostname, | ||
sdkURL: PEERBOARD_EMBED_SDK_URL, | ||
}; | ||
exports.createForum = function (forumID, container, options) { | ||
var opts = __assign(__assign({}, defaultOptions), { scrollToTopOnNavigationChanged: true }); | ||
if (options.prefix) { | ||
// Auto resolve redirect | ||
var prefixRgx = new RegExp("^/" + trimLeftSlash(options.prefix)); | ||
var pathnameWithoutPrefix = document.location.pathname.replace(prefixRgx, ''); | ||
opts.path = pathnameWithoutPrefix + document.location.search + document.location.hash; | ||
} | ||
Object.assign(opts, options); | ||
return exports.loadSdk(options.sdkURL).then(function () { | ||
if (!forumSDK) { | ||
throw new Error("Forum should be loaded at the moment."); | ||
} | ||
forumSDK.createForum(forumID, container, opts); | ||
}).catch(function (err) { | ||
console.error("Error creating forum: ", err); | ||
if (options.onFail) { | ||
options.onFail(); | ||
} | ||
}); | ||
}; | ||
//# sourceMappingURL=peerboard-core.js.map |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/*! ***************************************************************************** | ||
@@ -29,3 +31,6 @@ Copyright (c) Microsoft Corporation. | ||
var PEERBOARD_EMBED_SDK_URL = 'https://static.peerboard.org/embed/embed.js'; | ||
var PEERBOARD_EMBED_SDK_URL = 'https://static.peerboard.com/embed/embed.js'; | ||
var trimLeftSlash = function (str) { | ||
return str.startsWith('/') ? str.substr(1) : str; | ||
}; | ||
var forumSDK = null; | ||
@@ -57,21 +62,32 @@ var loadingSDK = null; | ||
}; | ||
var peerboardCore = { | ||
loadSdk: loadSdk, | ||
createForum: function (forumID, container, options) { | ||
var opts = __assign({ resize: true, hideMenu: true, scrollTarget: 'top', baseURL: options.baseURL || "https://peerboard." + window.document.location.hostname }, options); | ||
return loadSdk(options.sdkURL || PEERBOARD_EMBED_SDK_URL).then(function () { | ||
if (!forumSDK) { | ||
throw new Error("Forum should be loaded at the moment."); | ||
} | ||
forumSDK.createForum(forumID, container, opts); | ||
}).catch(function (err) { | ||
console.error("Error creating forum: ", err); | ||
if (options.onFail) { | ||
options.onFail(); | ||
} | ||
}); | ||
var defaultOptions = { | ||
resize: true, | ||
hideMenu: true, | ||
baseURL: "https://peerboard." + window.document.location.hostname, | ||
sdkURL: PEERBOARD_EMBED_SDK_URL, | ||
}; | ||
var createForum = function (forumID, container, options) { | ||
var opts = __assign(__assign({}, defaultOptions), { scrollToTopOnNavigationChanged: true }); | ||
if (options.prefix) { | ||
// Auto resolve redirect | ||
var prefixRgx = new RegExp("^/" + trimLeftSlash(options.prefix)); | ||
var pathnameWithoutPrefix = document.location.pathname.replace(prefixRgx, ''); | ||
opts.path = pathnameWithoutPrefix + document.location.search + document.location.hash; | ||
} | ||
Object.assign(opts, options); | ||
return loadSdk(options.sdkURL).then(function () { | ||
if (!forumSDK) { | ||
throw new Error("Forum should be loaded at the moment."); | ||
} | ||
forumSDK.createForum(forumID, container, opts); | ||
}).catch(function (err) { | ||
console.error("Error creating forum: ", err); | ||
if (options.onFail) { | ||
options.onFail(); | ||
} | ||
}); | ||
}; | ||
module.exports = peerboardCore; | ||
exports.loadSdk = loadSdk; | ||
exports.createForum = createForum; | ||
//# sourceMappingURL=peerboard-core.cjs.js.map |
@@ -1,6 +0,5 @@ | ||
export declare type ScrollTarget = 'iframe' | 'top'; | ||
interface Options { | ||
prefix?: string; | ||
jwtToken?: string; | ||
minHeight?: string; | ||
minHeight?: number | string; | ||
onPathChanged?: (forumPath: string) => void; | ||
@@ -11,18 +10,35 @@ onTitleChanged?: (title: string) => void; | ||
onFail?: () => void; | ||
path?: string; | ||
baseURL?: string; | ||
sdkURL?: string; | ||
resize?: boolean; | ||
scrollTarget?: ScrollTarget; | ||
hideMenu?: boolean; | ||
} | ||
interface InternalSDKOptions { | ||
prefix?: string; | ||
prefixProxy?: string; | ||
baseURL?: string; | ||
jwtToken?: string; | ||
wpPayload?: string; | ||
path?: string; | ||
resize?: boolean; | ||
minHeight?: string | number; | ||
hideMenu?: boolean; | ||
disableViewportSync?: boolean; | ||
onPathChanged?: (forumPath: string) => void; | ||
onTitleChanged?: (title: string) => void; | ||
onCustomProfile?: (url: string) => void; | ||
onReady?: () => void; | ||
onFail?: () => void; | ||
scrollToTopOnNavigationChanged?: boolean; | ||
sendReferrer?: boolean; | ||
} | ||
export interface ForumAPI { | ||
destroy(): void; | ||
} | ||
export interface PeerboardSDK { | ||
createForum(forumID: number, container: HTMLElement, options: Options): ForumAPI; | ||
export interface PeerboardSDKEmbedScript { | ||
createForum(forumID: number, container: HTMLElement, options: InternalSDKOptions): ForumAPI; | ||
} | ||
declare const _default: { | ||
loadSdk: (embedSDKURL?: string | undefined) => Promise<void>; | ||
createForum(forumID: number, container: HTMLElement, options: Options): Promise<void>; | ||
}; | ||
export default _default; | ||
export declare const loadSdk: (embedSDKURL?: string | undefined) => Promise<void>; | ||
export declare const createForum: (forumID: number, container: HTMLElement, options: Readonly<Options>) => Promise<void>; | ||
export {}; |
{ | ||
"name": "@peerboard/core", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "peerboard.com frontend components", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,7 +0,7 @@ | ||
### Usage | ||
### Peerboard core | ||
See [more detailed tutorial in our community](https://community.peerboard.com/post/1162435747) | ||
See [detailed tutorial in our community](https://community.peerboard.com/post/1162435747) | ||
```bash | ||
yarn add @peerboard/core | ||
yarn add @peerboard/core | ||
``` | ||
@@ -12,74 +12,1 @@ or | ||
``` | ||
#### Example integration into react application | ||
```jsx | ||
// ... | ||
import { createForum } from '@peerboard/core'; | ||
class Forum extends React.Component { | ||
containerRef = React.createRef(); | ||
constructor(props) { | ||
super(props); | ||
this.jwtToken = null; | ||
this.prefix = 'community'; | ||
this.state = { | ||
authReady: false, | ||
forumReady: false, | ||
error: null, | ||
}; | ||
} | ||
componentDidMount() { | ||
http.generateBearerToken((document.location.pathname || "/").replace('/' + this.prefix, '')).then((result) => { | ||
this.jwtToken = result.token; | ||
this.setState({ | ||
authReady: true, | ||
}); | ||
createForum(413170950, this.containerRef.current, { | ||
prefix: this.prefix, | ||
jwtToken: this.jwtToken, | ||
minHeight: "900px", | ||
onReady: () => { | ||
this.setState({ | ||
forumReady: true, | ||
}); | ||
}, | ||
onFail: () => { | ||
this.setState({ | ||
error: "Failed to load forum", | ||
}); | ||
}, | ||
onTitleChanged: (title) => window.document.title = "Community: " + title, | ||
onPathChanged: (location) => { | ||
// Browser counts iframe state changes. | ||
this.props.history.replace(location); | ||
}, | ||
onCustomProfile: (url) => { | ||
this.props.history.push(url.replace(window.location.origin, '')); | ||
}, | ||
}); | ||
}) | ||
} | ||
renderForum() { | ||
return <div> | ||
{!(this.state.authReady && this.state.forumReady) && 'Loading...'} | ||
<div ref={this.containerRef} style={{ | ||
visibility: this.state.forumReady ? 'visible' : 'hidden', | ||
}}> | ||
</div> | ||
</div> | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
{this.state.error ? (this.state.error) : (this.renderForum())} | ||
</div> | ||
); | ||
} | ||
} | ||
// ... | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20081
11.76%198
29.41%12
-85.88%