Spaces:
Running
Running
Download components/extensions/UniversalVideoExtension.ts from codebybrody/codebydante: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/spaces/codebybrody/codebydante/resolve/269ea4c5aa9c8ed277c8c5953a3a48b04d280430/components/extensions/UniversalVideoExtension.ts
- Command line
-
hf download hf://spaces/codebybrody/codebydante@269ea4c5aa9c8ed277c8c5953a3a48b04d280430/components/extensions/UniversalVideoExtension.ts
-
curl -L -o UniversalVideoExtension.ts https://huggingface.co/spaces/codebybrody/codebydante/resolve/269ea4c5aa9c8ed277c8c5953a3a48b04d280430/components/extensions/UniversalVideoExtension.ts
2.52 kB
| import { Node, mergeAttributes, PasteRule } from '@tiptap/core'; | |
| import { ReactNodeViewRenderer } from '@tiptap/react'; | |
| import { ResizableVideoComponent } from './ResizableVideoComponent'; | |
| export const UniversalVideoExtension = Node.create({ | |
| name: 'universalVideo', | |
| group: 'block', | |
| atom: true, | |
| addAttributes() { | |
| return { | |
| src: { | |
| default: null, | |
| parseHTML: element => element.getAttribute('data-src'), | |
| renderHTML: attributes => ({ | |
| 'data-src': attributes.src, | |
| }), | |
| }, | |
| width: { | |
| default: '100%', | |
| parseHTML: element => element.getAttribute('data-width') || element.style.width, | |
| renderHTML: (attributes) => ({ | |
| 'data-width': attributes.width, | |
| style: `width: ${attributes.width}`, | |
| }), | |
| }, | |
| align: { | |
| default: 'center', | |
| parseHTML: element => element.getAttribute('data-align'), | |
| renderHTML: (attributes) => ({ | |
| 'data-align': attributes.align, | |
| style: `float: ${attributes.align === 'left' ? 'left' : attributes.align === 'right' ? 'right' : 'none'}; display: ${attributes.align === 'center' ? 'block' : 'inline-block'}; margin: ${attributes.align === 'center' ? '0 auto' : '0 1rem 0.5rem 0'}`, | |
| }), | |
| }, | |
| }; | |
| }, | |
| parseHTML() { | |
| return [ | |
| { | |
| tag: 'div[data-type="universal-video"]', | |
| }, | |
| ]; | |
| }, | |
| renderHTML({ HTMLAttributes }) { | |
| return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'universal-video' })]; | |
| }, | |
| addNodeView() { | |
| return ReactNodeViewRenderer(ResizableVideoComponent); | |
| }, | |
| addPasteRules() { | |
| return [ | |
| new PasteRule({ | |
| find: /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.+$/g, | |
| handler: ({ state, range, match }) => { | |
| const url = match[0]; | |
| let finalUrl = url; | |
| if (!finalUrl.match(/^https?:\/\//)) { | |
| finalUrl = `https://${finalUrl}`; | |
| } | |
| state.tr.replaceWith(range.from, range.to, this.type.create({ src: finalUrl })); | |
| }, | |
| }), | |
| ]; | |
| }, | |
| }); | |