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

feature: copy table in context menu #331

Merged
merged 1 commit into from
Jun 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/editor/contentState/copyCutCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cheerio from 'cheerio'
import selection from '../selection'
import { CLASS_OR_ID } from '../config'
import { getSanitizeHtml } from '../utils/exportUnstylishHtml'
import ExportMarkdown from '../utils/exportMarkdown'

const copyCutCtrl = ContentState => {
ContentState.prototype.cutHandler = function () {
Expand Down Expand Up @@ -147,6 +148,14 @@ const copyCutCtrl = ContentState => {
event.clipboardData.setData('text/plain', getSanitizeHtml(text))
break
}
case 'copyTable': {
const table = this.getTableBlock()
if (!table) return
const markdown = new ExportMarkdown([ table ]).generate()
event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', markdown)
break
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/editor/contentState/tableBlockCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ const tableBlockCtrl = ContentState => {
this.eventCenter.dispatch('stateChange')
}

ContentState.prototype.getTableBlock = function () {
const { start, end } = this.cursor
const startBlock = this.getBlock(start.key)
const endBlock = this.getBlock(end.key)
const startParents = this.getParents(startBlock)
const endParents = this.getParents(endBlock)
const affiliation = startParents
.filter(p => endParents.includes(p))

if (affiliation.length) {
const table = affiliation.find(p => p.type === 'figure')
return table
}
}

ContentState.prototype.tableBlockUpdate = function (block) {
const { type } = block
if (type !== 'p') return false
Expand Down
11 changes: 11 additions & 0 deletions src/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,17 @@ class Aganippe {
document.execCommand('paste')
}

copy (name) {
switch (name) {
case 'table':
this._copyType = 'copyTable'
document.execCommand('copy')
break
default:
break
}
}

destroy () {
this.emoji.clear() // clear emoji cache for memory recycle
this.contentState.clear()
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/components/editorWithTabs/editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
bus.$on('insertParagraph', this.handleInsertParagraph)
bus.$on('editTable', this.handleEditTable)
bus.$on('scroll-to-header', this.scrollToHeader)
bus.$on('copy-block', this.handleCopyBlock)

// when cursor is in `![](cursor)` will emit `insert-image`
this.editor.on('insert-image', type => {
Expand Down Expand Up @@ -410,6 +411,10 @@

blurEditor () {
this.editor.blur()
},

handleCopyBlock (name) {
this.editor.copy(name)
}
},

Expand All @@ -432,6 +437,7 @@
bus.$off('insertParagraph', this.handleInsertParagraph)
bus.$off('editTable', this.handleEditTable)
bus.$off('scroll-to-header', this.scrollToHeader)
bus.$off('copy-block', this.handleCopyBlock)

this.editor.destroy()
this.editor = null
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/contextMenu/editor/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import bus from '../../bus'

export const copyTable = () => {
bus.$emit('copy-block', 'table')
}

export const copyAsMarkdown = (menuItem, browserWindow) => {
bus.$emit('copyAsMarkdown', 'copyAsMarkdown')
}
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/contextMenu/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CUT,
COPY,
PASTE,
COPY_TABLE,
COPY_AS_MARKDOWN,
COPY_AS_HTML,
PASTE_AS_PLAIN_TEXT,
Expand All @@ -29,6 +30,8 @@ export const showContextMenu = (event, { start, end }) => {
REMOVE_ROW,
INSERT_COLUMN,
REMOVE_COLUMN,
SEPARATOR,
COPY_TABLE,
SEPARATOR
)
}
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/contextMenu/editor/menuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export const PASTE = {
role: 'paste'
}

export const COPY_TABLE = {
label: 'Copy Table',
id: 'copyTableMenuItem',
click (menuItem, browserWindow) {
contextMenu.copyTable()
}
}

export const COPY_AS_MARKDOWN = {
label: 'Copy As Markdown',
id: 'copyAsMarkdownMenuItem',
Expand Down