Merge pull request #1480 from atom/ks-return-object-from-pasteboard-read

Clean up atom.pasteboard
This commit is contained in:
Kevin Sawicki 2014-02-03 13:56:16 -08:00
commit cb8075206b
12 changed files with 104 additions and 90 deletions

View File

@ -77,7 +77,7 @@
"feedback": "0.22.0",
"find-and-replace": "0.81.0",
"fuzzy-finder": "0.32.0",
"gists": "0.15.0",
"gists": "0.16.0",
"git-diff": "0.23.0",
"github-sign-in": "0.18.0",
"go-to-line": "0.16.0",
@ -89,7 +89,7 @@
"metrics": "0.24.0",
"package-generator": "0.25.0",
"release-notes": "0.17.0",
"settings-view": "0.64.0",
"settings-view": "0.65.0",
"snippets": "0.24.0",
"spell-check": "0.21.0",
"status-bar": "0.32.0",
@ -98,8 +98,8 @@
"tabs": "0.18.0",
"terminal": "0.27.0",
"timecop": "0.13.0",
"to-the-hubs": "0.18.0",
"tree-view": "0.66.0",
"to-the-hubs": "0.19.0",
"tree-view": "0.67.0",
"update-package-dependencies": "0.2.0",
"visual-bell": "0.6.0",
"welcome": "0.4.0",

View File

@ -0,0 +1,12 @@
describe "Clipboard", ->
describe "write(text, metadata) and read()", ->
it "writes and reads text to/from the native clipboard", ->
expect(atom.clipboard.read()).toBe 'initial clipboard content'
atom.clipboard.write('next')
expect(atom.clipboard.read()).toBe 'next'
it "returns metadata if the item on the native clipboard matches the last written item", ->
atom.clipboard.write('next', {meta: 'data'})
expect(atom.clipboard.read()).toBe 'next'
expect(atom.clipboard.readWithMetadata().text).toBe 'next'
expect(atom.clipboard.readWithMetadata().metadata).toEqual {meta: 'data'}

View File

@ -1856,12 +1856,12 @@ describe "Editor", ->
expect(editor.getCursorBufferPosition()).toEqual [0, 2]
expect(editor.getCursorScreenPosition()).toEqual [0, editor.getTabLength() * 2]
describe "pasteboard operations", ->
describe "clipboard operations", ->
beforeEach ->
editor.setSelectedBufferRanges([[[0, 4], [0, 13]], [[1, 6], [1, 10]]])
describe ".cutSelectedText()", ->
it "removes the selected text from the buffer and places it on the pasteboard", ->
it "removes the selected text from the buffer and places it on the clipboard", ->
editor.cutSelectedText()
expect(buffer.lineForRow(0)).toBe "var = function () {"
expect(buffer.lineForRow(1)).toBe " var = function(items) {"
@ -1885,7 +1885,7 @@ describe "Editor", ->
editor.cutToEndOfLine()
expect(buffer.lineForRow(2)).toBe ' if (items.length'
expect(buffer.lineForRow(3)).toBe ' var pivot = item'
expect(atom.pasteboard.read()[0]).toBe ' <= 1) return items;\ns.shift(), current, left = [], right = [];'
expect(atom.clipboard.read()).toBe ' <= 1) return items;\ns.shift(), current, left = [], right = [];'
describe "when text is selected", ->
it "only cuts the selected text, not to the end of the line", ->
@ -1895,7 +1895,7 @@ describe "Editor", ->
expect(buffer.lineForRow(2)).toBe ' if (items.lengthurn items;'
expect(buffer.lineForRow(3)).toBe ' var pivot = item'
expect(atom.pasteboard.read()[0]).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];'
expect(atom.clipboard.read()).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];'
describe ".copySelectedText()", ->
it "copies selected text onto the clipboard", ->
@ -1906,7 +1906,7 @@ describe "Editor", ->
describe ".pasteText()", ->
it "pastes text into the buffer", ->
atom.pasteboard.write('first')
atom.clipboard.write('first')
editor.pasteText()
expect(editor.buffer.lineForRow(0)).toBe "var first = function () {"
expect(buffer.lineForRow(1)).toBe " var first = function(items) {"

View File

@ -2515,9 +2515,9 @@ describe "EditorView", ->
expect(edited).toBe false
describe "when editor:copy-path is triggered", ->
it "copies the absolute path to the editor view's file to the pasteboard", ->
it "copies the absolute path to the editor view's file to the clipboard", ->
editorView.trigger 'editor:copy-path'
expect(atom.pasteboard.read()[0]).toBe editor.getPath()
expect(atom.clipboard.read()).toBe editor.getPath()
describe "when editor:move-line-up is triggered", ->
describe "when there is no selection", ->

View File

@ -1,10 +0,0 @@
describe "Pasteboard", ->
describe "write(text, metadata) and read()", ->
it "writes and reads text to/from the native pasteboard", ->
expect(atom.pasteboard.read()).toEqual ['initial pasteboard content']
atom.pasteboard.write('next')
expect(atom.pasteboard.read()[0]).toBe 'next'
it "returns metadata if the item on the native pasteboard matches the last written item", ->
atom.pasteboard.write('next', {meta: 'data'})
expect(atom.pasteboard.read()).toEqual ['next', {meta: 'data'}]

View File

@ -95,9 +95,9 @@ beforeEach ->
TokenizedBuffer.prototype.chunkSize = Infinity
spyOn(TokenizedBuffer.prototype, "tokenizeInBackground").andCallFake -> @tokenizeNextChunk()
pasteboardContent = 'initial pasteboard content'
spyOn(clipboard, 'writeText').andCallFake (text) -> pasteboardContent = text
spyOn(clipboard, 'readText').andCallFake -> pasteboardContent
clipboardContent = 'initial clipboard content'
spyOn(clipboard, 'writeText').andCallFake (text) -> clipboardContent = text
spyOn(clipboard, 'readText').andCallFake -> clipboardContent
addCustomMatchers(this)

View File

@ -28,7 +28,7 @@ WindowEventHandler = require './window-event-handler'
# * `atom.menu` - A {MenuManager} instance
# * `atom.workspaceView` - A {WorkspaceView} instance
# * `atom.packages` - A {PackageManager} instance
# * `atom.pasteboard` - A {Pasteboard} instance
# * `atom.clipboard` - A {Clipboard} instance
# * `atom.project` - A {Project} instance
# * `atom.syntax` - A {Syntax} instance
# * `atom.themes` - A {ThemeManager} instance
@ -134,7 +134,7 @@ class Atom extends Model
Config = require './config'
Keymap = require './keymap'
PackageManager = require './package-manager'
Pasteboard = require './pasteboard'
Clipboard = require './clipboard'
Syntax = require './syntax'
ThemeManager = require './theme-manager'
ContextMenuManager = require './context-menu-manager'
@ -148,7 +148,8 @@ class Atom extends Model
@themes = new ThemeManager({packageManager: @packages, configDirPath, resourcePath})
@contextMenu = new ContextMenuManager(devMode)
@menu = new MenuManager({resourcePath})
@pasteboard = new Pasteboard()
@clipboard = new Clipboard()
@syntax = @deserializers.deserialize(@state.syntax) ? new Syntax()
@subscribe @packages, 'activated', => @watchThemes()

48
src/clipboard.coffee Normal file
View File

@ -0,0 +1,48 @@
clipboard = require 'clipboard'
crypto = require 'crypto'
# Public: Represents the clipboard used for copying and pasting in Atom.
#
# A clipboard instance is always available under the `atom.clipboard` global.
module.exports =
class Clipboard
metadata: null
signatureForMetadata: null
# Creates an `md5` hash of some text.
#
# * text: A {String} to hash.
#
# Returns a hashed {String}.
md5: (text) ->
crypto.createHash('md5').update(text, 'utf8').digest('hex')
# Public: Write the given text to the clipboard.
#
# The metadata associated with the text is available by calling
# {.readWithMetadata}.
#
# * text: A {String} to store.
# * metadata: An {Object} of additional info to associate with the text.
write: (text, metadata) ->
@signatureForMetadata = @md5(text)
@metadata = metadata
clipboard.writeText(text)
# Public: Read the text from the clipboard.
#
# Returns a {String}.
read: ->
clipboard.readText()
# Public: Read the text from the clipboard and return both the text and the
# associated metadata.
#
# Returns an {Object} with a `text` key and a `metadata` key if it has
# associated metadata.
readWithMetadata: ->
text = @read()
if @signatureForMetadata is @md5(text)
{text, @metadata}
else
{text}

View File

@ -209,7 +209,7 @@ class EditorView extends View
'editor:toggle-line-comments': => @toggleLineCommentsInSelection()
'editor:log-cursor-scope': => @logCursorScope()
'editor:checkout-head-revision': => @checkoutHead()
'editor:copy-path': => @copyPathToPasteboard()
'editor:copy-path': => @copyPathToClipboard()
'editor:move-line-up': => @editor.moveLineUp()
'editor:move-line-down': => @editor.moveLineDown()
'editor:duplicate-line': => @editor.duplicateLine()
@ -1411,9 +1411,9 @@ class EditorView extends View
@highlightedLine = null
# Copies the current file path to the native clipboard.
copyPathToPasteboard: ->
copyPathToClipboard: ->
path = @editor.getPath()
atom.pasteboard.write(path) if path?
atom.clipboard.write(path) if path?
### Internal ###

View File

@ -535,31 +535,31 @@ class Editor extends Model
# Public: Copies and removes all characters from cursor to the end of the
# line.
cutToEndOfLine: ->
maintainPasteboard = false
maintainClipboard = false
@mutateSelectedText (selection) ->
selection.cutToEndOfLine(maintainPasteboard)
maintainPasteboard = true
selection.cutToEndOfLine(maintainClipboard)
maintainClipboard = true
# Public: Cuts the selected text.
cutSelectedText: ->
maintainPasteboard = false
maintainClipboard = false
@mutateSelectedText (selection) ->
selection.cut(maintainPasteboard)
maintainPasteboard = true
selection.cut(maintainClipboard)
maintainClipboard = true
# Public: Copies the selected text.
copySelectedText: ->
maintainPasteboard = false
maintainClipboard = false
for selection in @getSelections()
selection.copy(maintainPasteboard)
maintainPasteboard = true
selection.copy(maintainClipboard)
maintainClipboard = true
# Public: Pastes the text in the clipboard.
#
# * options:
# + A set of options equivalent to {Selection.insertText}.
pasteText: (options={}) ->
[text, metadata] = atom.pasteboard.read()
{text, metadata} = atom.clipboard.readWithMetadata()
containsNewlines = text.indexOf('\n') isnt -1

View File

@ -1,36 +0,0 @@
clipboard = require 'clipboard'
crypto = require 'crypto'
# Public: Represents the clipboard used for copying and pasting in Atom.
#
# A pasteboard instance is always available under the `atom.pasteboard` global.
module.exports =
class Pasteboard
signatureForMetadata: null
# Creates an `md5` hash of some text.
#
# text - A {String} to encrypt.
#
# Returns an encrypted {String}.
md5: (text) ->
crypto.createHash('md5').update(text, 'utf8').digest('hex')
# Public: Write the given text to the clipboard.
#
# text - A {String} to store.
# metadata - An {Object} of additional info to associate with the text.
write: (text, metadata) ->
@signatureForMetadata = @md5(text)
@metadata = metadata
clipboard.writeText(text)
# Public: Read the text from the clipboard.
#
# Returns an {Array}. The first element is the saved text and the second is
# any metadata associated with the text.
read: ->
text = clipboard.readText()
value = [text]
value.push(@metadata) if @signatureForMetadata == @md5(text)
value

View File

@ -506,34 +506,33 @@ class Selection
# Public: Cuts the selection until the end of the line.
#
# * maintainPasteboard:
# * maintainClipboard:
# ?
cutToEndOfLine: (maintainPasteboard) ->
cutToEndOfLine: (maintainClipboard) ->
@selectToEndOfLine() if @isEmpty()
@cut(maintainPasteboard)
@cut(maintainClipboard)
# Public: Copies the selection to the pasteboard and then deletes it.
# Public: Copies the selection to the clipboard and then deletes it.
#
# * maintainPasteboard:
# * maintainClipboard:
# ?
cut: (maintainPasteboard=false) ->
@copy(maintainPasteboard)
cut: (maintainClipboard=false) ->
@copy(maintainClipboard)
@delete()
# Public: Copies the current selection to the pasteboard.
# Public: Copies the current selection to the clipboard.
#
# * maintainPasteboard:
# * maintainClipboard:
# ?
copy: (maintainPasteboard=false) ->
copy: (maintainClipboard=false) ->
return if @isEmpty()
text = @editor.buffer.getTextInRange(@getBufferRange())
if maintainPasteboard
[currentText, metadata] = atom.pasteboard.read()
text = currentText + '\n' + text
if maintainClipboard
text = "#{atom.clipboard.read()}\n#{text}"
else
metadata = { indentBasis: @editor.indentationForBufferRow(@getBufferRange().start.row) }
atom.pasteboard.write(text, metadata)
atom.clipboard.write(text, metadata)
# Public: Creates a fold containing the current selection.
fold: ->