Files
Peter EvansandJeremie Pardou 5e271e22c9 feat: add permanent workspace invite links (#6521)
* feat: add permanent workspace invite links behind the rbac_improvements feature flag

* feat: ask logged-in users to confirm before joining a workspace through an invite link

* feat: make the invite link code length configurable with a default of 12 and a minimum of 7

* feat: let admins name invite links so they remember what each one is for

* feat: show only the invite path in the link column to make room for the name

* fix: harden invite links and let people sign up through them with single sign-on

* fix: ask people who sign up through an invite link with single sign-on to confirm joining

* feat: let invite links raise a member's role after they confirm, without ever lowering it

* fix: move shared clipboard helper into core

* fix: validate workspace invitation link roles

* refactor: share confirmation modal across core features

* refactor: use literal frontend translation keys

* fix: label invitation route Invite links

* fix: copy invitation links when clicking their paths

* fix: align invitation migration with latest develop

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-04 08:37:21 +01:00

109 lines
4.0 KiB
JavaScript

import { registerTrustedImageUrlsFromMarkdown } from '@baserow/modules/core/editor/trustedImageUrls'
export { copyToClipboard } from '@baserow/modules/core/utils/clipboard'
export const LOCAL_STORAGE_CLIPBOARD_KEY = 'baserow.clipboardData'
/**
* Returns the rich clipboard data stored for the exact plain-text clipboard value.
* The browser clipboard cannot contain Baserow's field metadata, so grid copies keep
* that metadata in local storage and use the plain text as an integrity check.
*
* @param {string} textRawData The current plain-text clipboard value.
* @returns {Array|null} The matching two-dimensional rich clipboard data.
*/
export const getStoredRichClipboardData = (textRawData) => {
try {
const clipboardData = JSON.parse(
localStorage.getItem(LOCAL_STORAGE_CLIPBOARD_KEY)
)
const clipboardDataTextToCompare = clipboardData.text.replaceAll(
'\r\n',
'\n'
)
const textRawDataToCompare = textRawData.replaceAll('\r\n', '\n')
if (clipboardDataTextToCompare === textRawDataToCompare) {
return clipboardData.json
}
} catch (e) {
// Invalid or inaccessible local storage is equivalent to no rich clipboard data.
}
try {
localStorage.removeItem(LOCAL_STORAGE_CLIPBOARD_KEY)
} catch (e) {
// There is nothing else to clear when local storage itself is inaccessible.
}
return null
}
/**
* Returns Markdown when the clipboard contains one rich-text grid cell. This is used
* by database-hosted rich-text editors, which otherwise only see the quoted TSV text.
*
* @param {string} textRawData The current plain-text clipboard value.
* @returns {string|null} The original Markdown or null for any other clipboard shape.
*/
export const getRichTextClipboardContent = (textRawData) => {
const richClipboardData = getStoredRichClipboardData(textRawData)
if (richClipboardData?.length !== 1 || richClipboardData[0]?.length !== 1) {
return null
}
const richValue = richClipboardData[0][0]
if (!richValue?.richText || typeof richValue.value !== 'string') {
return null
}
// The value is a grid cell the backend resolved, so its image URLs may be
// loaded by the editor it is pasted into.
registerTrustedImageUrlsFromMarkdown(richValue.value)
return richValue.value
}
/**
* This method gets the text and json data from the clipboard and from the local
* storage. This is needed because we need the original metadata to be able to
* restore the original format, but the clipboard only allows to store plain
* text, html or images related mime types.
* @param {*} event
* @returns {object} An object with the textRawData and jsonRawData. The
* textRawData is the plain text that is stored in the clipboard. The
* jsonRawData is the rich json object with all the metadata needed to restore
* the original data in the correct rich format.
*/
export const getRichClipboard = async (event) => {
let textRawData
if (typeof navigator.clipboard?.readtText !== 'undefined') {
textRawData = await navigator.clipboard.readText()
} else {
textRawData = event.clipboardData.getData('text/plain')
}
const jsonRawData = getStoredRichClipboardData(textRawData)
return { textRawData: textRawData.trim(), jsonRawData }
}
/**
* DEPRECATED: Kept for backward compatibility where the clipboard API is not available.
* Values should be an object with mime types as key and clipboard content for this type
* as value. This allow add the same data with multiple representation to the clipboard.
* We can have a row saved as tsv string or as json string. Values must be strings.
* @param {object} values object of mime types -> clipboard content.
*/
export const setRichClipboard = (values) => {
const listener = (e) => {
e.preventDefault()
e.stopPropagation()
Object.entries(values).forEach(([type, content]) => {
e.clipboardData.setData(type, content)
})
}
document.addEventListener('copy', listener)
try {
document.execCommand('copy')
} finally {
document.removeEventListener('copy', listener)
}
}