-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tiptap): Loom embed extension (#8612)
* feat(tiptap): Loom embed extension * Use read-only approach * Increase margins + refactor HTML a bit * Fix/improve regexp
- Loading branch information
1 parent
9b92924
commit e1e6958
Showing
3 changed files
with
99 additions
and
2 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
89 changes: 89 additions & 0 deletions
89
packages/client/components/promptResponse/loomExtension.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,89 @@ | ||
import {Node} from '@tiptap/core' | ||
import {JSONContent, mergeAttributes} from '@tiptap/react' | ||
|
||
const LOOM_REGEX = /^(?:https?:\/\/)?(?:www\.)?loom\.com\/share\/[a-zA-Z0-9]+/g | ||
|
||
export const unfurlLoomLinks = (rawContent: JSONContent): JSONContent => { | ||
if (!rawContent.content) { | ||
return rawContent | ||
} | ||
|
||
const newContent: JSONContent[] = [] | ||
rawContent.content.forEach((content) => { | ||
if (content.type === 'paragraph') { | ||
newContent.push(content, ...getLoomNodesForParagraph(content)) | ||
} else { | ||
newContent.push(unfurlLoomLinks(content)) | ||
} | ||
}) | ||
|
||
return { | ||
type: rawContent.type, | ||
content: newContent | ||
} | ||
} | ||
|
||
const getLoomNodesForParagraph = (content: JSONContent) => { | ||
const distinctLoomLinks: Record<string, string> = {} | ||
content.content?.forEach((subContent) => { | ||
if (subContent.type === 'text') { | ||
const link = subContent.marks?.find((mark) => mark.type === 'link') | ||
if (!link) { | ||
return | ||
} | ||
|
||
if (link.attrs?.href.match(LOOM_REGEX)) { | ||
const linkKey = link.attrs.href.split('?')[0] | ||
if (!distinctLoomLinks[linkKey]) { | ||
distinctLoomLinks[linkKey] = link.attrs.href | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return Object.values(distinctLoomLinks).map((link) => { | ||
return { | ||
type: 'loom', | ||
attrs: { | ||
src: link | ||
} | ||
} | ||
}) | ||
} | ||
|
||
export const LoomExtension = Node.create({ | ||
name: 'loom', | ||
|
||
group: 'block', | ||
inline: false, | ||
|
||
addAttributes() { | ||
return { | ||
src: { | ||
default: null | ||
} | ||
} | ||
}, | ||
|
||
renderHTML(props) { | ||
const {HTMLAttributes} = props | ||
const components = HTMLAttributes.src.split('/') | ||
return [ | ||
'div', | ||
mergeAttributes(HTMLAttributes, { | ||
style: 'position: relative; padding-bottom: 75%; height: 0; margin: 8px 0px;' | ||
}), | ||
[ | ||
'iframe', | ||
{ | ||
src: `https://www.loom.com/embed/${components[components.length - 1]}`, | ||
style: 'position: absolute; top: 0; left: 0; width: 100%; height: 100%;', | ||
frameborder: '0', | ||
webkitallowfullscreen: true, | ||
mozallowfullscreen: true, | ||
allowfullscreen: 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