-
-
Notifications
You must be signed in to change notification settings - Fork 32
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 #46 from hunghg255/feat-attachment
- Loading branch information
Showing
17 changed files
with
457 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
description: Attachment | ||
--- | ||
|
||
# Attachment | ||
|
||
Attachment is a node extension that allows you to add an Attachment to your editor. | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { Attachment } from 'reactjs-tiptap-editor'; // [!code ++] | ||
|
||
const extensions = [ | ||
..., | ||
// Import Extensions Here | ||
Attachment.configure({// [!code ++] | ||
upload: (file: any) => {// [!code ++] | ||
// upload file to server return url | ||
},// [!code ++] | ||
}),// [!code ++] | ||
]; | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,106 @@ | ||
import { Node, mergeAttributes } from '@tiptap/core' | ||
import { ReactNodeViewRenderer } from '@tiptap/react' | ||
|
||
import { getDatasetAttribute } from '@/utils/dom-dataset' | ||
import { NodeViewAttachment } from '@/extensions/Attachment/components/NodeViewAttachment/NodeViewAttachment' | ||
import { ActionButton } from '@/components' | ||
import type { GeneralOptions } from '@/types' | ||
|
||
declare module '@tiptap/core' { | ||
interface Commands<ReturnType> { | ||
attachment: { | ||
setAttachment: (attrs?: unknown) => ReturnType | ||
} | ||
} | ||
} | ||
|
||
export interface AttachmentOptions extends GeneralOptions<AttachmentOptions> { | ||
/** Function for uploading files */ | ||
upload?: (file: File) => Promise<string> | ||
} | ||
|
||
export const Attachment = Node.create<AttachmentOptions>({ | ||
name: 'attachment', | ||
content: '', | ||
marks: '', | ||
group: 'block', | ||
selectable: true, | ||
atom: true, | ||
draggable: true, | ||
|
||
addOptions() { | ||
return { | ||
...this.parent?.(), | ||
HTMLAttributes: { | ||
class: 'attachment', | ||
}, | ||
button: ({ editor, t }: any) => ({ | ||
component: ActionButton, | ||
componentProps: { | ||
action: () => editor.chain().focus().setAttachment().run(), | ||
isActive: () => false, | ||
disabled: false, | ||
icon: 'Attachment', | ||
tooltip: t('editor.attachment.tooltip'), | ||
}, | ||
}), | ||
} | ||
}, | ||
|
||
parseHTML() { | ||
return [{ tag: 'div[class=attachment]' }] | ||
}, | ||
|
||
renderHTML({ HTMLAttributes }) { | ||
// @ts-expect-error | ||
return ['div', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)] | ||
}, | ||
|
||
addAttributes() { | ||
return { | ||
fileName: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('filename'), | ||
}, | ||
fileSize: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('filesize'), | ||
}, | ||
fileType: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('filetype'), | ||
}, | ||
fileExt: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('fileext'), | ||
}, | ||
url: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('url'), | ||
}, | ||
hasTrigger: { | ||
default: false, | ||
parseHTML: element => getDatasetAttribute('hastrigger')(element) === 'true', | ||
}, | ||
error: { | ||
default: null, | ||
parseHTML: getDatasetAttribute('error'), | ||
}, | ||
} | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
setAttachment: | ||
(attrs = {}) => | ||
({ chain }) => { | ||
// @ts-expect-error | ||
return chain().insertContent({ type: this.name, attrs }).run() | ||
}, | ||
} | ||
}, | ||
|
||
addNodeView() { | ||
return ReactNodeViewRenderer(NodeViewAttachment) | ||
}, | ||
}) |
38 changes: 38 additions & 0 deletions
38
src/extensions/Attachment/components/NodeViewAttachment/FileIcon.tsx
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,38 @@ | ||
import { LucideAudioLines, LucideFile, LucideImage, LucideSheet, LucideTableProperties, LucideVideo } from 'lucide-react' | ||
import { normalizeFileType } from '@/utils/file' | ||
import { ExportPdf } from '@/components/icons/ExportPdf' | ||
import ExportWord from '@/components/icons/ExportWord' | ||
|
||
export function getFileTypeIcon(fileType: string) { | ||
const type = normalizeFileType(fileType) | ||
|
||
switch (type) { | ||
case 'audio': | ||
return <LucideAudioLines /> | ||
|
||
case 'video': | ||
return <LucideVideo /> | ||
|
||
case 'file': | ||
return <LucideFile /> | ||
|
||
case 'image': | ||
return <LucideImage /> | ||
|
||
case 'pdf': | ||
return <ExportPdf /> | ||
|
||
case 'word': | ||
return <ExportWord /> | ||
|
||
case 'excel': | ||
return <LucideSheet /> | ||
|
||
case 'ppt': | ||
return <LucideTableProperties /> | ||
|
||
default: { | ||
return <></> | ||
} | ||
} | ||
} |
Oops, something went wrong.