Skip to content

Commit

Permalink
fixed copying as HTML - fixes #8092, fixes #8091
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Mar 19, 2023
1 parent beb7c61 commit 2e72774
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 61 deletions.
58 changes: 1 addition & 57 deletions tabby-terminal/src/frontends/xtermFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { ImageAddon } from 'xterm-addon-image'
import { CanvasAddon } from 'xterm-addon-canvas'
import './xterm.css'
import deepEqual from 'deep-equal'
import { Attributes } from 'xterm/src/common/buffer/Constants'
import { AttributeData } from 'xterm/src/common/buffer/AttributeData'
import { CellData } from 'xterm/src/common/buffer/CellData'
import { BaseTerminalProfile, TerminalColorScheme } from '../api/interfaces'

const COLOR_NAMES = [
Expand Down Expand Up @@ -492,60 +489,7 @@ export class XTermFrontend extends Frontend {
}

private getSelectionAsHTML (): string {
let html = `<div style="font-family: '${this.configService.store.terminal.font}', monospace; white-space: pre">`
const selection = this.xterm.getSelectionPosition()
if (!selection) {
return ''
}
if (selection.start.y === selection.end.y) {
html += this.getLineAsHTML(selection.start.y, selection.start.x, selection.end.x)
} else {
html += this.getLineAsHTML(selection.start.y, selection.start.x, this.xterm.cols)
for (let y = selection.start.y + 1; y < selection.end.y; y++) {
html += this.getLineAsHTML(y, 0, this.xterm.cols)
}
html += this.getLineAsHTML(selection.end.y, 0, selection.end.x)
}
html += '</div>'
return html
}

private getHexColor (mode: number, color: number, def: string): string {
if (mode === Attributes.CM_RGB) {
const rgb = AttributeData.toColorRGB(color)
return rgb.map(x => x.toString(16).padStart(2, '0')).join('')
}
if (mode === Attributes.CM_P16 || mode === Attributes.CM_P256) {
return this.configService.store.terminal.colorScheme.colors[color]
}
return def
}

private getLineAsHTML (y: number, start: number, end: number): string {
let html = '<div>'
let lastStyle: string|null = null
const outerLine = this.xterm.buffer.active.getLine(y)
if (!outerLine) {
return ''
}
const line = outerLine['_line']
const cell = new CellData()
for (let i = start; i < end; i++) {
line.loadCell(i, cell)
const fg = this.getHexColor(cell.getFgColorMode(), cell.getFgColor(), this.configService.store.terminal.colorScheme.foreground)
const bg = this.getHexColor(cell.getBgColorMode(), cell.getBgColor(), this.configService.store.terminal.colorScheme.background)
const style = `color: ${fg}; background: ${bg}; font-weight: ${cell.isBold() ? 'bold' : 'normal'}; font-style: ${cell.isItalic() ? 'italic' : 'normal'}; text-decoration: ${cell.isUnderline() ? 'underline' : 'none'}`
if (style !== lastStyle) {
if (lastStyle !== null) {
html += '</span>'
}
html += `<span style="${style}">`
lastStyle = style
}
html += line.getString(i) || ' '
}
html += '</span></div>'
return html
return this.serializeAddon.serializeAsHTML({ includeGlobalBackground: true, onlySelection: true })
}
}

Expand Down
1 change: 0 additions & 1 deletion tabby-terminal/webpack.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ export default () => {
},
],
})
cfg.resolve.modules.push('node_modules/xterm/src')
return cfg
}
3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
"../../app/node_modules/*",
"./app/node_modules/*"
],
"common*": [
"../../tabby-terminal/node_modules/xterm/src/common*"
],
"tabby-*": ["../../tabby-*/src"],
}
},
Expand Down

0 comments on commit 2e72774

Please sign in to comment.