forked from Doist/typist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the
PasteHTMLTableAsString
extension (Doist#290)
* feat: Add the `PasteSpreadsheetTable` extension * Rename extension to be more explicit on what it does * Add support for multiple copied GFM tables * Do not paste images there are multiple clipboard types Apparently, Numbers (Apple's spreadsheet app) copies content as text, HTML, and an image (for some reason), and the `RichTextImage` will take precedence over `PasteHTMLTableAsString` and paste an image instead of pasting the Numbers table as text/HTML. This changes prevents that from happening by making sure that the `RichTextImage` extension will only handle images if only images are found in the clipboard data, otherwise the event will not be handled, and passed on to the next handler in the chain.
- Loading branch information
Showing
6 changed files
with
113 additions
and
1 deletion.
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
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
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,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 } |
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