-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4354 from serlo/feat/bildungsraum-share-copy-edit…
…or-content feat(bildungsraum-share): copy content to clipboard
- Loading branch information
Showing
15 changed files
with
325 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { parseDocumentString } from '@editor/static-renderer/helper/parse-document-string' | ||
import type { | ||
EditorArticleDocument, | ||
EditorExerciseDocument, | ||
EditorExerciseGroupDocument, | ||
} from '@editor/types/editor-plugins' | ||
import { gql } from 'graphql-request' | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
import { endpoint } from '@/api/endpoint' | ||
import { InjectionOnlyContentQuery } from '@/fetcher/graphql-types/operations' | ||
import { isProduction } from '@/helper/is-production' | ||
|
||
/** | ||
* Allows frontend to copy Serlo content to the clipboard. | ||
* The content is unpacked for consistent pasting in the Editor. | ||
*/ | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const href = decodeURIComponent(String(req.query.href)) | ||
|
||
const [base] = href.split('#') | ||
const path = base.startsWith('/') ? base : `/${base}` | ||
|
||
if (!path) { | ||
return res.status(401).json('no path provided') | ||
} | ||
|
||
try { | ||
void fetch(endpoint, { | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify({ query, variables: { path } }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data: { data: InjectionOnlyContentQuery }) => { | ||
if (!data.data?.uuid) { | ||
return res.status(404).json('not found') | ||
} | ||
|
||
const uuid = data.data.uuid | ||
|
||
if (!Object.hasOwn(uuid, 'currentRevision') || !uuid.currentRevision) { | ||
return res.status(404).json('no current revision') | ||
} | ||
|
||
if (uuid.__typename === 'Article') { | ||
const articleDocument = parseDocumentString( | ||
uuid.currentRevision.content | ||
) as EditorArticleDocument | ||
const articleContent = articleDocument.state.content | ||
respondWithContent(articleContent) | ||
return | ||
} | ||
|
||
if ( | ||
uuid.__typename === 'Exercise' || | ||
uuid.__typename === 'ExerciseGroup' | ||
) { | ||
const exercise = parseDocumentString(uuid.currentRevision.content) as | ||
| EditorExerciseDocument | ||
| EditorExerciseGroupDocument | ||
respondWithContent({ plugin: 'rows', state: [exercise] }) | ||
return | ||
} | ||
|
||
return res.status(422).json('unsupported entity type') | ||
}) | ||
.catch((e) => { | ||
return res.status(500).json(`${String(e)} at ${path}`) | ||
}) | ||
} catch (e) { | ||
return res.status(500).json(`${String(e)} at ${path}`) | ||
} | ||
|
||
function respondWithContent(content: any) { | ||
const twoDaysInSeconds = 172800 | ||
res.setHeader('Cache-Control', `maxage=${twoDaysInSeconds}`) | ||
if (!isProduction) res.setHeader('Access-Control-Allow-Origin', '*') | ||
res.status(200).json(content) | ||
} | ||
} | ||
|
||
const query = gql` | ||
query shareEditorContent($path: String!) { | ||
uuid(alias: { path: $path, instance: de }) { | ||
__typename | ||
... on AbstractEntity { | ||
currentRevision { | ||
content | ||
} | ||
} | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/editor/src/plugin/helpers/unsupported-plugin-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const eventName = 'unsupportedPluginType' | ||
|
||
const event = new Event(eventName) | ||
|
||
export function emitUnsupportedPluginsEvent() { | ||
document.dispatchEvent(event) | ||
} | ||
|
||
export function listenForUnsupportedPlugins(callback: () => void) { | ||
document.addEventListener(eventName, callback, { capture: true, once: true }) | ||
} | ||
|
||
export function removeUnsupportedPluginsListener(callback: () => void) { | ||
document.removeEventListener(eventName, callback, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.