From 50517431db6eefc6b62b33f5e73b880a9368a0f0 Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Mon, 4 Aug 2014 23:02:54 +0200 Subject: [PATCH 1/3] added support for inserting snippets by shift+click --- app/views/play/level/tome/SpellPaletteEntryView.coffee | 3 +++ app/views/play/level/tome/SpellView.coffee | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/app/views/play/level/tome/SpellPaletteEntryView.coffee b/app/views/play/level/tome/SpellPaletteEntryView.coffee index 3b7f53c71..ee3b2e892 100644 --- a/app/views/play/level/tome/SpellPaletteEntryView.coffee +++ b/app/views/play/level/tome/SpellPaletteEntryView.coffee @@ -71,6 +71,9 @@ module.exports = class SpellPaletteEntryView extends CocoView Backbone.Mediator.publish 'tome:palette-pin-toggled', entry: @, pinned: @popoverPinned onClick: (e) => + if key.shift + Backbone.Mediator.publish 'tome:insert-snippet', doc: @options.doc, language: @options.language + return @togglePinned() Backbone.Mediator.publish 'tome:palette-clicked', thang: @thang, prop: @doc.name, entry: @ diff --git a/app/views/play/level/tome/SpellView.coffee b/app/views/play/level/tome/SpellView.coffee index ee979cc38..a00f72126 100644 --- a/app/views/play/level/tome/SpellView.coffee +++ b/app/views/play/level/tome/SpellView.coffee @@ -47,6 +47,7 @@ module.exports = class SpellView extends CocoView 'tome:change-language': 'onChangeLanguage' 'tome:change-config': 'onChangeEditorConfig' 'tome:update-snippets': 'addZatannaSnippets' + 'tome:insert-snippet': 'onInsertSnippet' 'spell-beautify': 'onSpellBeautify' events: @@ -661,6 +662,13 @@ module.exports = class SpellView extends CocoView @spell.setLanguage e.language @reloadCode true if wasDefault + onInsertSnippet: (e) -> + console.log 'doc', e.doc + return unless e.doc.snippets?[e.language]?.code + snippetManager = ace.require('ace/snippets').snippetManager + snippetManager.insertSnippet @ace, e.doc.snippets[e.language].code + return + dismiss: -> @spell.hasChangedSignificantly @getSource(), null, (hasChanged) => @recompile() if hasChanged From 84c0fbd01fb65d484f50cbd88d971042994411de Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Tue, 5 Aug 2014 11:19:15 +0200 Subject: [PATCH 2/3] use shortName as fallback for some entries if no snippet --- .../play/level/tome/SpellPaletteEntryView.coffee | 2 +- app/views/play/level/tome/SpellView.coffee | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/views/play/level/tome/SpellPaletteEntryView.coffee b/app/views/play/level/tome/SpellPaletteEntryView.coffee index ee3b2e892..39a1d164e 100644 --- a/app/views/play/level/tome/SpellPaletteEntryView.coffee +++ b/app/views/play/level/tome/SpellPaletteEntryView.coffee @@ -72,7 +72,7 @@ module.exports = class SpellPaletteEntryView extends CocoView onClick: (e) => if key.shift - Backbone.Mediator.publish 'tome:insert-snippet', doc: @options.doc, language: @options.language + Backbone.Mediator.publish 'tome:insert-snippet', doc: @options.doc, language: @options.language, formatted: @doc return @togglePinned() Backbone.Mediator.publish 'tome:palette-clicked', thang: @thang, prop: @doc.name, entry: @ diff --git a/app/views/play/level/tome/SpellView.coffee b/app/views/play/level/tome/SpellView.coffee index a00f72126..11ad5dcc8 100644 --- a/app/views/play/level/tome/SpellView.coffee +++ b/app/views/play/level/tome/SpellView.coffee @@ -663,10 +663,15 @@ module.exports = class SpellView extends CocoView @reloadCode true if wasDefault onInsertSnippet: (e) -> - console.log 'doc', e.doc - return unless e.doc.snippets?[e.language]?.code + console.log 'doc', e.doc, e.formatted + snippetCode = null + if e.doc.snippets?[e.language]?.code + snippetCode = e.doc.snippets[e.language].code + else if (e.formatted.type isnt 'snippet') and e.formatted.shortName? + snippetCode = e.formatted.shortName + return unless snippetCode? snippetManager = ace.require('ace/snippets').snippetManager - snippetManager.insertSnippet @ace, e.doc.snippets[e.language].code + snippetManager.insertSnippet @ace, snippetCode return dismiss: -> From d2c63f4b98427774140995ef5cb17782a1330480 Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Mon, 11 Aug 2014 21:34:02 +0200 Subject: [PATCH 3/3] fixed undo manager to not remove all code --- app/views/play/level/tome/SpellView.coffee | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/views/play/level/tome/SpellView.coffee b/app/views/play/level/tome/SpellView.coffee index 11ad5dcc8..8a4c38f89 100644 --- a/app/views/play/level/tome/SpellView.coffee +++ b/app/views/play/level/tome/SpellView.coffee @@ -3,6 +3,7 @@ template = require 'templates/play/level/tome/spell' {me} = require 'lib/auth' filters = require 'lib/image_filter' Range = ace.require('ace/range').Range +UndoManager = ace.require('ace/undomanager').UndoManager Problem = require './Problem' SpellDebugView = require './SpellDebugView' SpellToolbarView = require './SpellToolbarView' @@ -175,6 +176,7 @@ module.exports = class SpellView extends CocoView fillACE: -> @ace.setValue @spell.source + @aceSession.setUndoManager(new UndoManager()) @ace.clearSelection() addZatannaSnippets: (e) -> @@ -209,6 +211,7 @@ module.exports = class SpellView extends CocoView @loaded = false @previousSource = @ace.getValue() @ace.setValue('') + @aceSession.setUndoManager(new UndoManager()) fireURL = 'https://codecombat.firebaseio.com/' + @spell.pathComponents.join('/') @fireRef = new Firebase fireURL firepadOptions = userId: me.id @@ -223,6 +226,7 @@ module.exports = class SpellView extends CocoView @spell.source = firepadSource else @ace.setValue @previousSource + @aceSession.setUndoManager(new UndoManager()) @ace.clearSelection() @onAllLoaded() @@ -303,6 +307,7 @@ module.exports = class SpellView extends CocoView @firepad.setText source else @ace.setValue source + @aceSession.setUndoManager(new UndoManager()) @eventsSuppressed = false try @ace.resize true # hack: @ace may not have updated its text properly, so we force it to refresh