Skip to content

Commit

Permalink
Merge branch 'develop' into fix/upgrade-node-relay-10
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-funk committed Jun 26, 2023
2 parents 55e514c + 4164b99 commit bda20d0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
14 changes: 14 additions & 0 deletions docs/docs/comment-embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ Add the `commentEmbed.js` script to your `html` tree. On a page that includes th
></script>
```

If you are manually including the embed script, and you want to set a `customCSSURL` and a `customFontsCSSURL` to use instead or in place of tenant settings for these values, include these as data attributes on the script for the single comment embed to use. If the script is automatically added by the _Stream Embed_, these values will be grabbed and set for you.

Example of how to manually add them:

```html
<script
class="coral-script"
src="//{{ CORAL_DOMAIN_NAME }}/assets/js/commentEmbed.js"
data-customCSSURL="{{ CUSTOM_CSS_URL }}"
data-customFontsCSSURL="{{ CUSTOM_FONTS_CSS_URL }}"
defer
></script>
```

> **NOTE:** Replace the value of `{{ CORAL_DOMAIN_NAME }}` with the location of your running instance of Coral.
### Embed code
Expand Down
2 changes: 1 addition & 1 deletion src/core/client/embed/StreamEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class StreamEmbed {
injectCountScriptIfNeeded(config.rootURL, this.ts);

// Detect if comment embed injection is needed and add the comment embed script.
injectCommentEmbedScriptIfNeeded(config.rootURL, this.ts);
injectCommentEmbedScriptIfNeeded(config, this.ts);

if (config.commentID) {
// Delay emit of `showPermalink` event to allow user enough time to setup
Expand Down
13 changes: 11 additions & 2 deletions src/core/client/embed/injectCommentEmbedScriptIfNeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import {
} from "coral-framework/constants";
import { detectCommentEmbedScript } from "coral-framework/helpers";

import { StreamEmbedConfig } from "./StreamEmbed";

/**
* injectCommentEmbedScriptIfNeeded will detect if comment embed injection is necessary and
* automatically includes the `commentEmbed.js` script.
*/
const injectCommentEmbedScriptIfNeeded = (
rootURL: string,
config: StreamEmbedConfig,
timestamp: number
) => {
if (detectCommentEmbedScript(window)) {
return;
}
if (document.querySelector(COMMENT_EMBED_SELECTOR)) {
const s = document.createElement("script");
s.src = `${rootURL}/assets/js/commentEmbed.js?ts=${timestamp}`;
s.src = `${config.rootURL}/assets/js/commentEmbed.js?ts=${timestamp}`;
s.className = ORIGIN_FALLBACK_ID;
s.setAttribute("id", "coralSingleCommentEmbedScript");
if (config.customCSSURL) {
s.setAttribute("data-customCSSURL", config.customCSSURL);
}
if (config.customFontsCSSURL) {
s.setAttribute("data-customFontsCSSURL", config.customFontsCSSURL);
}
s.async = false;
s.defer = true;
(document.head || document.body).appendChild(s);
Expand Down
27 changes: 25 additions & 2 deletions src/core/client/oembed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface CommentEmbedQueryArgs {
commentID?: string;
allowReplies?: string;
reactionLabel?: string;
customFontsCSSURL?: string | null;
customCSSURL?: string | null;
}

/** createCommentEmbedQueryRef creates a unique reference from the query args */
Expand All @@ -34,9 +36,22 @@ function detectAndInject(opts: DetectAndInjectArgs = {}) {
const commentID = element.dataset.commentid;
const allowReplies = element.dataset.allowreplies;
const reactionLabel = element.dataset.reactionlabel;
const embedScript = document.querySelector(
"#coralSingleCommentEmbedScript"
);
const customCSSURL = embedScript?.getAttribute("data-customCSSURL");
const customFontsCSSURL = embedScript?.getAttribute(
"data-customFontsCSSURL"
);

// Construct the args for generating the ref.
const args = { commentID, allowReplies, reactionLabel };
const args = {
commentID,
allowReplies,
reactionLabel,
customCSSURL,
customFontsCSSURL,
};

// Get or create a ref.
let ref = element.dataset.coralRef;
Expand All @@ -59,14 +74,22 @@ function detectAndInject(opts: DetectAndInjectArgs = {}) {

// Call server using JSONP.
Object.keys(queryMap).forEach((ref) => {
const { commentID, allowReplies, reactionLabel } = queryMap[ref];
const {
commentID,
allowReplies,
reactionLabel,
customCSSURL,
customFontsCSSURL,
} = queryMap[ref];

// Compile the arguments used to generate the
const args: Record<string, string | undefined> = {
allowReplies,
commentID,
ref,
reactionLabel,
customCSSURL: customCSSURL ?? undefined,
customFontsCSSURL: customFontsCSSURL ?? undefined,
};

// Add the script element with the specified options to the page.
Expand Down
10 changes: 8 additions & 2 deletions src/core/server/app/handlers/api/comment/commentEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const CommentEmbedJSONPQuerySchema = Joi.object().keys({
commentID: Joi.string().required(),
allowReplies: Joi.string().optional(),
reactionLabel: Joi.string().optional(),
customCSSURL: Joi.string().optional(),
customFontsCSSURL: Joi.string().optional(),
ref: Joi.string().required(),
});

Expand All @@ -46,6 +48,8 @@ interface CommentEmbedJSONPQuery {
ref: string;
allowReplies?: string;
reactionLabel?: string;
customCSSURL?: string;
customFontsCSSURL?: string;
}

/**
Expand Down Expand Up @@ -78,6 +82,8 @@ export const commentEmbedJSONPHandler =
ref,
allowReplies,
reactionLabel,
customCSSURL: customCSSURLEmbed,
customFontsCSSURL: customFontsCSSURLEmbed,
}: CommentEmbedJSONPQuery = validate(
CommentEmbedJSONPQuerySchema,
req.query
Expand Down Expand Up @@ -147,7 +153,7 @@ export const commentEmbedJSONPHandler =
mediaUrl,
includeReplies,
streamCSS,
customCSSURL,
customCSSURL: customCSSURLEmbed || customCSSURL,
staticRoot: staticURI || tenantURL,
giphyMedia,
tenantURL,
Expand All @@ -164,7 +170,7 @@ export const commentEmbedJSONPHandler =
const data: CommentEmbedJSONPData = {
ref,
html,
customFontsCSSURL,
customFontsCSSURL: customFontsCSSURLEmbed || customFontsCSSURL,
defaultFontsCSSURL,
commentID,
staticRoot: staticURI || tenantURL,
Expand Down

0 comments on commit bda20d0

Please sign in to comment.