Skip to content

Commit

Permalink
🐎 Move deserializers and view providers in package.json
Browse files Browse the repository at this point in the history
This allow Atom to not require the main module unless it actually needs
to deserialize something
  • Loading branch information
abe33 committed Aug 26, 2016
1 parent 672a0d8 commit d4ab79c
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 203 deletions.
18 changes: 6 additions & 12 deletions lib/csv-editor-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ class CSVEditorElement extends HTMLElement
createFormView: ->
return if @form?

CSVEditorFormElement ?= require './csv-editor-form-element'
CSVPreviewElement ?= require './csv-preview-element'

@removeTableEditor()

@formContainer = document.createElement('div')
@formContainer.className = 'settings-view'

@form = document.createElement 'atom-csv-editor-form'
@form = new CSVEditorFormElement
@formSubscriptions = new CompositeDisposable

@formSubscriptions.add @subscribeTo @form.openTextEditorButton,
Expand Down Expand Up @@ -141,6 +144,8 @@ class CSVEditorElement extends HTMLElement
@displayProgress() unless @progress?

displayProgress: ->
CSVProgressElement ?= require './csv-progress-element'

@progress = new CSVProgressElement
@appendChild(@progress)

Expand All @@ -159,17 +164,6 @@ module.exports =
CSVEditorElement =
registerOrUpdateElement 'atom-csv-editor', CSVEditorElement.prototype

CSVEditorElement.registerViewProvider = ->
CSVEditorFormElement ?= require './csv-editor-form-element'
CSVPreviewElement ?= require './csv-preview-element'
CSVProgressElement ?= require './csv-progress-element'
CSVEditor ?= require './csv-editor'

atom.views.addViewProvider CSVEditor, (model) ->
element = new CSVEditorElement
element.setModel(model)
element

atom.commands.add 'atom-csv-editor',
'core:save-as': (e) ->
unless @model.editor?
Expand Down
50 changes: 0 additions & 50 deletions lib/csv-editor-placeholder.coffee

This file was deleted.

4 changes: 1 addition & 3 deletions lib/csv-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class CSVEditor
csvEditor.applyChoice()
csvEditor

atom.deserializers.add(this)

@tableEditorForPath: {}

constructor: (state={}) ->
Expand Down Expand Up @@ -443,7 +441,7 @@ class CSVEditor

serialize: ->
out = {
deserializer: 'CSVEditorPlaceholder'
deserializer: 'CSVEditor'
filePath: @getPath()
@options
@choice
Expand Down
2 changes: 0 additions & 2 deletions lib/display-table.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class DisplayTable
state.table = atom.deserializers.deserialize(state.table) if state.table?
displayTable = new DisplayTable(state)

atom.deserializers.add(this)

@delegatesMethods(
'changeColumnName', 'undo', 'redo', 'getRows', 'getColumns','getColumnCount', 'getColumnIndex', 'getRowCount', 'clearUndoStack', 'clearRedoStack', 'getValueAtPosition', 'swapColumns', 'setValueAtPosition', 'setValuesAtPositions', 'setValuesInRange', 'rowRangeFrom', 'swapRows', 'getRow',
toProperty: 'table'
Expand Down
2 changes: 0 additions & 2 deletions lib/table-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class TableEditor

new TableEditor(state)

atom.deserializers.add(this)

@delegatesProperties(
'order', 'direction',
toProperty: 'displayTable'
Expand Down
7 changes: 0 additions & 7 deletions lib/table-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1568,13 +1568,6 @@ module.exports =
TableElement =
registerOrUpdateElement 'tablr-editor', TableElement.prototype

TableElement.registerViewProvider = ->
atom.views.addViewProvider TableEditor, (model) ->
element = new TableElement
element.setModel(model)
element


# ###### ## ## ########
# ## ## ### ### ## ##
# ## #### #### ## ##
Expand Down
6 changes: 0 additions & 6 deletions lib/table-selection-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,3 @@ class TableSelectionElement extends HTMLElement
module.exports =
TableSelectionElement =
registerOrUpdateElement 'tablr-editor-selection', TableSelectionElement.prototype

TableSelectionElement.registerViewProvider = ->
atom.views.addViewProvider Selection, (model) ->
element = new TableSelectionElement
element.setModel(model)
element
2 changes: 0 additions & 2 deletions lib/table.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class Table
table.initializeAfterSetup()
table

atom.deserializers.add(this)

constructor: (state={}) ->
{@id, @columns, @rows, modified} = state
@cachedContents = state.cachedContents ? '' if modified
Expand Down
72 changes: 36 additions & 36 deletions lib/tablr.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
encodings = require './encodings'
[_, url, CompositeDisposable, Range, Table, DisplayTable, TableEditor, TableElement, TableSelectionElement, CSVConfig, CSVEditor, CSVEditorElement] = []

{CSVEditorPlaceholder, CSVEditorPlaceholderElement} = require './csv-editor-placeholder'

atom.deserializers.add(CSVEditorPlaceholder)
CSVEditorPlaceholderElement.registerViewProvider()
[_, url, CompositeDisposable, Range, Table, DisplayTable, TableEditor, Selection, TableElement, TableSelectionElement, CSVConfig, CSVEditor, CSVEditorElement] = []

module.exports =
activate: ({csvConfig}) ->
Expand All @@ -30,8 +25,6 @@ module.exports =
_ ?= require 'underscore-plus'
CSVEditor ?= require './csv-editor'

@registerViews()

choice = @csvConfig.get(uriToOpen, 'choice')
options = _.clone(@csvConfig.get(uriToOpen, 'options') ? {})

Expand Down Expand Up @@ -81,8 +74,6 @@ module.exports =
, 10
}]

@replacePlaceholders()

deactivate: ->
@subscriptions.dispose()

Expand All @@ -94,22 +85,43 @@ module.exports =

{Table, DisplayTable, TableEditor, Range}

replacePlaceholders: ->
@registerViews()
placeholders = atom.workspace.getPaneItems()
.filter (item) -> item instanceof CSVEditorPlaceholder
.forEach (item) ->
pane = atom.workspace.paneForItem(item)
itemIndex = pane.getItems().indexOf(item)
isActive = pane.getActiveItem() is item
csvEditor = item.getCSVEditor()
pane.removeItem(item)
pane.addItem(csvEditor, index: itemIndex)
pane.activateItem(csvEditor) if isActive
deserializeCSVEditor: (state) ->
CSVEditor ?= require './csv-editor'
CSVEditor.deserialize(state)

getSmallTable: ->
@registerViews()
deserializeTableEditor: (state) ->
TableEditor ?= require './table-editor'
TableEditor.deserialize(state)

deserializeDisplayTable: (state) ->
DisplayTable ?= require './display-table'
DisplayTable.deserialize(state)

deserializeTable: (state) ->
Table ?= require './table'
Table.deserialize(state)

tablrViewProvider: (model) ->
TableEditor ?= require './table-editor'
Selection ?= require './selection'
CSVEditor ?= require './csv-editor'

element = if model instanceof TableEditor
TableElement ?= require './table-element'
new TableElement

else if model instanceof Selection
TableSelectionElement ?= require './table-selection-element'
new TableSelectionElement

else if model instanceof CSVEditor
CSVEditorElement ?= require './csv-editor-element'
new CSVEditorElement

element.setModel(model) if element?
element

getSmallTable: ->
TableEditor ?= require './table-editor'

table = new TableEditor
Expand All @@ -135,8 +147,6 @@ module.exports =
return table

getLargeTable: ->
@registerViews()

TableEditor ?= require './table-editor'

table = new TableEditor
Expand Down Expand Up @@ -169,15 +179,5 @@ module.exports =

return table

registerViews: ->
unless CSVEditorElement?
TableElement ?= require './table-element'
TableSelectionElement ?= require './table-selection-element'
CSVEditorElement ?= require './csv-editor-element'

CSVEditorElement.registerViewProvider()
TableElement.registerViewProvider()
TableSelectionElement.registerViewProvider()

serialize: ->
csvConfig: @csvConfig.serialize()
35 changes: 22 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
}
}
},
"dependencies": {
"atom-utils": "0.9.x",
"underscore-plus": "^1",
"mixto": "1.x",
"property-accessors": "1.x",
"delegato": "1.x",
"coffee-script": "1.7.x",
"csv": "~0.4.5",
"iconv-lite": "~0.4.4"
},
"devDependencies": {
"temp": "~0.8.0",
"fs-plus": "^2.8.1"
"deserializers": {
"CSVEditor": "deserializeCSVEditor",
"TableEditor": "deserializeTableEditor",
"DisplayTable": "deserializeDisplayTable",
"Table": "deserializeTable"
},
"viewProviders": [
"tablrViewProvider"
],
"configSchema": {
"tableEditor": {
"type": "object",
Expand Down Expand Up @@ -296,5 +291,19 @@
],
"description": "When file has no header, select the default naming method for the columns. `alphabetic` means use A, B,…, Z, AA, AB… `numeric` is for simple numbers, ie 1, 2… `numericZeroBased` is similar to `numeric`, except that it starts numbering from 0 instead of 1"
}
},
"dependencies": {
"atom-utils": "0.9.x",
"underscore-plus": "^1",
"mixto": "1.x",
"property-accessors": "1.x",
"delegato": "1.x",
"coffee-script": "1.7.x",
"csv": "~0.4.5",
"iconv-lite": "~0.4.4"
},
"devDependencies": {
"temp": "~0.8.0",
"fs-plus": "^2.8.1"
}
}
43 changes: 0 additions & 43 deletions spec/csv-editor-placeholder-spec.coffee

This file was deleted.

Loading

0 comments on commit d4ab79c

Please sign in to comment.