Skip to content

Commit

Permalink
copy embed code (#16)
Browse files Browse the repository at this point in the history
* copy embed code

* fix lint

* add a TODO

* get license name
  • Loading branch information
tomaskikutis authored Apr 11, 2024
1 parent 77422ff commit b4d5178
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
2 changes: 0 additions & 2 deletions client/app.js

This file was deleted.

52 changes: 52 additions & 0 deletions client/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import ReactDOM from 'react-dom';
import {registerExtensions} from 'newsroom-core/assets/index';
import {CopyEmbedCodeBanner} from './copy-embed-code-banner';

registerExtensions({
prepareWirePreview: (previewHtmlElement, article) => {
for (const embedBlockHtmlElement of previewHtmlElement.querySelectorAll('.embed-block')) {
const embeddedArticleId = embedBlockHtmlElement.getAttribute('data-association-key');

if (embeddedArticleId == null) {
continue;
}

const embeddedArticle = article.associations?.[embeddedArticleId];

if (embeddedArticle == null) {
continue;
}

const licenseName = (embeddedArticle.subject ?? []).find(({scheme}) => scheme === 'licence_type')?.name;

if (licenseName == null) {
continue;
}

const div = document.createElement('div');

div.style.textAlign = 'center';

ReactDOM.render(
<CopyEmbedCodeBanner
license={licenseName}
onCopy={() => {
navigator.clipboard.writeText(embedBlockHtmlElement.innerHTML);
}}
/>,
div,
);

const parent = embedBlockHtmlElement.parentElement;

if (parent != null) {
parent.insertBefore(div, embedBlockHtmlElement.nextSibling);
}

}

return previewHtmlElement;

}
});
60 changes: 60 additions & 0 deletions client/copy-embed-code-banner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, {useState} from 'react';
import {exposed} from 'newsroom-core/assets/index';

const bannerStyle: React.CSSProperties = {
display: 'flex',
gap: 16,
alignItems: 'center',
background: 'var(--color-background--muted)',
padding: 'var(--space--1-5)',
border: '1px solid var(--color-line--light)',
borderRadius: 'var(--border-radius--m)',
color: 'var(--color-text--muted)',
marginTop: 16,
};

const textStyle: React.CSSProperties = {
margin: 0,
textAlign: 'justify',
lineHeight: 1.2,
fontSize: 'var(--text-size--medium)',
}

const {Button} = exposed.ui;
const {gettext} = exposed.locale;

interface IProps {
onCopy(): void;
license: string;
}

export function CopyEmbedCodeBanner(props: IProps) {
const initialLabel = gettext('copy');
const [label, setLabel] = useState(initialLabel);

return (
<div style={bannerStyle}>
<div>
<img src="/theme/360-Logo.png" style={{width: '100%', maxHeight: 32}} />
</div>

<p style={textStyle}>
{gettext('Copy this into your own story. Available under {{x}} license.', {x: props.license})}
</p>

<Button
text={label}
size="small"
onClick={() => {
props.onCopy();

setLabel(gettext('Copied!'));

setTimeout(() => {
setLabel(initialLabel);
}, 1000);
}}
/>
</div>
);
}
6 changes: 6 additions & 0 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "newsroom-core/tsconfig",
"include": [
"."
],
}
1 change: 0 additions & 1 deletion server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,3 @@
]

PUBLIC_DASHBOARD = True

Binary file added server/theme/360-Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b4d5178

Please sign in to comment.