This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
/
ace.coffee
69 lines (51 loc) · 2.15 KB
/
ace.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#= require ../vendor/angular
#= require ../services/modes
EditSession = require("ace/edit_session").EditSession
UndoManager = require("ace/undomanager").UndoManager
module = angular.module("plunker.ace", ["plunker.modes"])
module.directive "plunkerSession", ["$rootScope", "$timeout", "modes", ($rootScope, $timeout, modes) ->
restrict: "E"
require: "?ngModel"
template: """
<div style="display: none" ng-model="buffer.content"></div>
"""
replace: true
link: ($scope, el, attrs, ngModel) ->
buffer = $scope.buffer
session = new EditSession(buffer.content or "")
session.setTabSize(2)
session.setUseSoftTabs(true)
session.setUndoManager(new UndoManager())
session.setMode(mode.source) if mode = modes.findByFilename(buffer.filename)
ngModel.$render = ->
session.setValue(ngModel.$viewValue or "")
read = -> ngModel.$setViewValue(session.getValue())
session.on 'change', -> $scope.$apply(read)
read()
$scope.buffer.session = session
$scope.$on "$destroy", ->
$timeout -> $rootScope.$broadcast "buffer:remove", $scope.buffer
$scope.$watch "buffer.content", (content, old_content) ->
$rootScope.$broadcast "buffer:change:content", $scope.buffer, content, old_content
$scope.$watch "buffer.filename", (filename, old_filename) ->
session.setMode(mode.source) if mode = modes.findByFilename(buffer.filename)
$rootScope.$broadcast "buffer:change:filename", $scope.buffer, filename, old_filename
$scope.$watch "scratch.buffers.active()", (active) ->
if active == buffer
$scope.ace.setSession(session)
$scope.ace.focus()
$rootScope.$broadcast "buffer:add", $scope.buffer
]
module.directive "plunkerAce", ["modes", (modes) ->
restrict: "E"
template: """
<div class="editor-canvas">
<plunker-session ng-repeat="buffer in scratch.buffers.queue"></plunker-session>
</div>
"""
replace: true
link: ($scope, el, attrs, ngModel) ->
$scope.ace = ace.edit(el[0])
$scope.$on "layout:resize", ->
$scope.ace.resize()
]