Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the PasteHTMLTableAsString extension #290

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/constants/extension-priorities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const SUGGESTION_EXTENSION_PRIORITY = 1000
*/
const SMART_MARKDOWN_TYPING_PRIORITY = 110

/**
* Priority for the `PasteHTMLTableAsString` extension. This needs to be higher than most paste
* extensions (e.g., `PasteSinglelineText`, `PasteMarkdown`, etc.), so that the extension can first
* parse HTML tables that might exist in the clipboard data.
*/
const PASTE_EXTENSION_PRIORITY = 105

/**
* Priority for the `ViewEventHandlers` extension. This needs to be higher than the default for most
* of the built-in and official extensions (i.e. `100`), so that the event handlers from the
Expand All @@ -28,6 +35,7 @@ const BLOCKQUOTE_EXTENSION_PRIORITY = 101

export {
BLOCKQUOTE_EXTENSION_PRIORITY,
PASTE_EXTENSION_PRIORITY,
SMART_MARKDOWN_TYPING_PRIORITY,
SUGGESTION_EXTENSION_PRIORITY,
VIEW_EVENT_HANDLERS_PRIORITY,
Expand Down
11 changes: 11 additions & 0 deletions src/extensions/plain-text/plain-text-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Text } from '@tiptap/extension-text'
import { Typography } from '@tiptap/extension-typography'

import { CopyMarkdownSource } from '../shared/copy-markdown-source'
import { PasteHTMLTableAsString } from '../shared/paste-html-table-as-string'
import { PasteSinglelineText } from '../shared/paste-singleline-text'

import { SmartMarkdownTyping } from './smart-markdown-typing/smart-markdown-typing'
Expand Down Expand Up @@ -34,6 +35,11 @@ type PlainTextKitOptions = {
*/
paragraph: Partial<PlainTextParagraphOptions> | false

/**
* Set to `false` to disable the `PasteHTMLTableAsString` extension.
*/
pasteHTMLTableAsString: false

/**
* Set to `false` to disable the `Text` extension.
*/
Expand Down Expand Up @@ -72,6 +78,11 @@ const PlainTextKit = Extension.create<PlainTextKitOptions>({
? PasteSinglelineText
: PasteMultilineText,
)

if (this.options?.pasteHTMLTableAsString !== false) {
// Supports pasting tables (from spreadsheets and websites) into the editor
extensions.push(PasteHTMLTableAsString)
}
}

if (this.options.history !== false) {
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/rich-text/rich-text-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ const RichTextImage = Image.extend<RichTextImageOptions>({
return false
}

// Do not handle the event if there are multiple clipboard types
if ((event.clipboardData?.types || []).length > 1) {
return false
}

const pastedFiles = Array.from(event.clipboardData?.files || [])

// Do not handle the event if no files were pasted
Expand Down
11 changes: 11 additions & 0 deletions src/extensions/rich-text/rich-text-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Typography } from '@tiptap/extension-typography'

import { BLOCKQUOTE_EXTENSION_PRIORITY } from '../../constants/extension-priorities'
import { CopyMarkdownSource } from '../shared/copy-markdown-source'
import { PasteHTMLTableAsString } from '../shared/paste-html-table-as-string'
import { PasteSinglelineText } from '../shared/paste-singleline-text'

import { BoldAndItalics } from './bold-and-italics'
Expand Down Expand Up @@ -159,6 +160,11 @@ type RichTextKitOptions = {
*/
pasteSinglelineText: false

/**
* Set to `false` to disable the `PasteHTMLTableAsString` extension.
*/
pasteHTMLTableAsString: false

/**
* Set options for the `Strike` extension, or `false` to disable.
*/
Expand Down Expand Up @@ -242,6 +248,11 @@ const RichTextKit = Extension.create<RichTextKitOptions>({
// pasted lines together
extensions.push(PasteSinglelineText)
}

if (this.options?.pasteHTMLTableAsString !== false) {
// Supports pasting tables (from spreadsheets and websites) into the editor
extensions.push(PasteHTMLTableAsString)
}
}

if (this.options.dropCursor !== false) {
Expand Down
77 changes: 77 additions & 0 deletions src/extensions/shared/paste-html-table-as-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Extension } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'

import { PASTE_EXTENSION_PRIORITY } from '../../constants/extension-priorities'
import { parseHtmlToElement } from '../../helpers/dom'

/**
* The `PasteHTMLTableAsString` extension adds the ability to paste a table copied from a spreadsheet
* web app (e.g., Google Sheets, Microsoft Excel), along with tables rendered by GitHub Flavored
* Markdown (GFM), into the editor.
*
* Since Typist does not yet support tables, this extension simply pastes the table as a string of
* paragraphs (one paragraph per row), with each cell separated by a space character. However,
* whenever we do add support for tables, this extension will need to be completely rewritten.
*
* Lastly, please note that formatting is lost when the copied table comes from Google Sheets or
* Microsoft Excel, because unfortunately, these apps style the cell contents using CSS.
*/
const PasteHTMLTableAsString = Extension.create({
name: 'pasteHTMLTableAsString',
priority: PASTE_EXTENSION_PRIORITY,
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('pasteHTMLTableAsString'),
props: {
transformPastedHTML(html) {
// Attempt to extract table(s) HTML from the pasted HTML
const tableHTML = html.match(/<table[^>]+>[\s\S]*?<\/table>/gi)

// Do not handle the event if no table HTML was found
if (!tableHTML) {
return html
}

// Concatenate all tables into a single string of paragraphs
return tableHTML.reduce((result, table) => {
const { firstElementChild: tableElement } = parseHtmlToElement(
table,
) as {
firstElementChild: HTMLTableElement | null
}

if (!tableElement) {
return result
}

// Transform the table element into a string of paragraphs
return (
result +
Array.from(tableElement.rows)
// Join each cell into a single string for each row
.reduce<string[]>((acc, row) => {
return [
...acc,
// Use `innerHTML` instead of `innerText` to preserve
// potential formatting (e.g., GFM) within each cell
Array.from(row.cells)
.map((cell) => cell.innerHTML)
.join(' '),
]
}, [])
// Discard rows that are completely empty
.filter((row) => row.trim().length > 0)
// Wrap each row in a paragraph
.map((row) => `<p>${row}</p>`)
.join('')
)
}, '')
},
},
}),
]
},
})

export { PasteHTMLTableAsString }
2 changes: 1 addition & 1 deletion src/extensions/shared/paste-singleline-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const PasteSinglelineText = Extension.create({
// Join break lines with a space character in-between
.replace(/<br>/g, ' ')
// Join paragraphs with a space character in-between
.replace(/<p[^>]+>(.*?)<\/p>/g, '$1 ')
.replace(/<p[^>]*>(.*?)<\/p>/g, '$1 ')

return isPlainTextDocument(view.state.schema)
? escape(bodyElement.innerText)
Expand Down