Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes and live editor #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8453949
initial commit
Radivarig Jun 14, 2015
80d755a
(add) active-markdown-source
Radivarig Jun 16, 2015
67b591b
(add) elementary checks
Radivarig Jun 16, 2015
58f4392
(add) coffee-loader and require resolving
Radivarig Jun 16, 2015
162aa46
(add) underscore, underscore.string
Radivarig Jun 16, 2015
31d53f8
(fix) markdown parsing, replaced with marked
Radivarig Jun 16, 2015
2fb799b
(add) fs
Radivarig Jun 16, 2015
f478a3e
(add) backbone
Radivarig Jun 16, 2015
857f457
(add) jquery
Radivarig Jun 16, 2015
49550d6
(add) live editing + example
Radivarig Jun 16, 2015
8a53157
(add) local reference to coffeescript library
Radivarig Jun 16, 2015
3a281fb
(add) codemirror (bug) text not displaying
Radivarig Jun 16, 2015
ed0172b
(fix) codemirror text not displaying
Radivarig Jun 16, 2015
f79775f
(update) local coffee-script
Radivarig Jun 16, 2015
6ed0b86
(clean) console logs
Radivarig Jun 16, 2015
2ac5b81
(remove) raw and download from Controls
Radivarig Jun 17, 2015
8f45e8a
(add) local scripts and style
Radivarig Jun 18, 2015
515306f
(fix) replaced codemirror with ace
Radivarig Jun 18, 2015
6d45b11
(fix) blockScrolling warning
Radivarig Jun 18, 2015
ffd6d88
(fix) spawning of new ace editor instance on every change
Radivarig Jun 18, 2015
22a2618
(fix) line numbers
Radivarig Jun 18, 2015
7d03ea4
(rm) unused library vega
Radivarig Jun 18, 2015
7f4805f
(chg) make debug visible
Radivarig Jun 19, 2015
20f02a3
(add) removing of variables undefined in parsing scope
Radivarig Jun 20, 2015
9f52af4
(add) try catch and console.log error
Radivarig Jun 20, 2015
ce62e83
(add) setting compile error msg to _compile_error_msg?.innerHTML
Radivarig Jun 20, 2015
62a09c2
(fix) on change rerender switch element
Radivarig Jun 20, 2015
e373ffd
(mv) drag_manager to range element
Radivarig Jun 20, 2015
bac2670
(mv) heading links to function (disable) heading links as it is not f…
Radivarig Jun 20, 2015
c687a58
(chg) export ActiveMarkdown and executor to window
Radivarig Jun 21, 2015
b4381d4
(fix) switch defaults to false
Radivarig Jun 26, 2015
1b25c7d
(fix) render string element after parse
Radivarig Jun 26, 2015
d8563cb
(fix) call deffered on input change
Radivarig Jun 26, 2015
62b9356
(fix) set string element value to text_content
Radivarig Jun 26, 2015
2a14d6c
(add) ace editor as input (add) displaying compile errors
Radivarig Jun 26, 2015
c74b9ab
(disable) controls element until fixed
Radivarig Jun 26, 2015
a2abf58
(fix) set error string in error element if empty
Radivarig Jun 26, 2015
f12aff2
(revert) since controls are disabled returning the code, for later re…
Radivarig Jun 26, 2015
5c28bd7
(cleanup) removed all template based code and deps, adapted live edit…
Radivarig Jun 27, 2015
16c240c
(add) exports.js for exporting browser script, and later command line
Radivarig Jun 27, 2015
970d792
Merge remote-tracking branch 'pullrequest/master' into HEAD
Radivarig Jun 27, 2015
3fe6208
(merge) package.json
Radivarig Jun 27, 2015
837154a
(fix) error div passes clicks
Radivarig Jun 27, 2015
64484c6
(rm) unused files
Radivarig Jun 27, 2015
b1c73b3
(fix) allowing any characters in text_content
Radivarig Jun 27, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./source/browser.coffee')
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="bundle.js" charset="utf-8"></script>
</body>
</html>
103 changes: 53 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
{
"name": "active-markdown",
"version": "0.3.2",
"description": "A tool for generating reactive documents from markdown source.",
"preferGlobal": true,
"main": "lib/ActiveMarkdown",
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
"name": "active-markdown",
"version": "0.3.2",
"description": "A tool for generating reactive documents from markdown source.",
"preferGlobal": true,
"main": "./exports.js",
"bin": {
"activemd": "./lib/command.js"
},
"scripts": {
"dev": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js --config webpack.config.js --progress --colors",
"test": "node_modules/.bin/mocha --compilers coffee:coffee-script"
},
"devDependencies": {
"autoprefixer-core": "^5.2.1",
"css-loader": "^0.9.1",
"jsx-loader": "^0.13.2",
"postcss-loader": "^0.4.4",
"react": "^0.13.3",
"style-loader": "^0.8.3",
"webpack": "^1.8.11",
"webpack-dev-server": "^1.8.2"
},
"dependencies": {
"backbone": "^1.2.1",
"brace": "^0.5.1",
"coffee-loader": "^0.7.2",
"coffee-script": "^1.9.3",
"fs": "0.0.2",
"jquery": "^2.1.4",
"marked": "^0.3.3",
"stylus-loader": "^1.2.1",
"underscore": "^1.8.3",
"underscore.string": "^3.1.1"
},
"author": "Alec Perkins <[email protected]>",
"contributors": [
{
"name": "Alec Perkins",
"email": "[email protected]"
},
"dependencies": {
"cli": "0.4.4-2",
"fs-extra": "0.6.x",
"showdown": "git://github.com/alecperkins/showdown.git#4d04b7855d5ed327ebbe5a917c9e9607a4188f77",
"underscore": "1.4.x",
"underscore.string": "2.3.x"
},
"devDependencies": {
"backbone": "1.0.x",
"browserify": "2.12.x",
"codemirror": "3.11.x",
"coffee-script": "1.6.x",
"d3": "3.1.5",
"mocha": "*",
"should": "*",
"sqwish": "0.2.x",
"stylus": "0.32.x",
"uglify-js": "2.2.x",
"zepto": "git://github.com/madrobby/zepto.git#v1.0"
},
"bin": {
"activemd": "./lib/command.js"
},
"scripts": {
"test": "node_modules/.bin/mocha --compilers coffee:coffee-script"
},
"repository": {
"type": "git",
"url": "git://github.com/alecperkins/active-markdown.git"
},
"author": "Alec Perkins <[email protected]>",
"license": {
"type": "Unlicense",
"url": "https://raw.github.com/alecperkins/active-markdown/master/UNLICENSE"
},
"contributors": [
{
"name": "Alec Perkins",
"email": "[email protected]"
}
]
}
{
"name": "Reslav Hollos",
"email": "[email protected]"
}
],
"license": {
"type": "Unlicense",
"url": "https://raw.github.com/alecperkins/active-markdown/master/UNLICENSE"
},
"repository": {
"type": "git",
"url": "git://github.com/alecperkins/active-markdown.git"
}
}
1 change: 1 addition & 0 deletions source/Controls.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NamedView = require './libraries/NamedView'
$ = require 'jquery'

class Controls extends NamedView
@_name: 'Controls'
Expand Down
2 changes: 1 addition & 1 deletion source/DragManager.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

$ = require 'jquery'

###
Handles drag operations- done globally to enable sliding action that starts on an element but continues across the window
Expand Down
24 changes: 14 additions & 10 deletions source/Executor.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
_ = require 'underscore'
{ Model } = require 'backbone'
CoffeeScript = require './libraries/coffee-script-1.9.3-min.js'

# Loaded in page separately
# CoffeeScript = require 'coffee-script'
Expand Down Expand Up @@ -38,7 +39,6 @@ class Executor
# the CoffeeScript compiler will include them in a single closure.
_.each @_code_blocks, (block, i) ->
line_count = coffee_code_str.split('\n').length
line_count += i
coffee_code_str += block.getSource(line_count) + '\n'
js_code_str = CoffeeScript.compile(coffee_code_str)
return js_code_str
Expand All @@ -48,17 +48,21 @@ class Executor
@_is_executing = true
_.defer =>
state = @_prepareState()
js_code_str = @_compileCode()
try
js_code_str = @_compileCode()
# Turn the code string into an actual function, and call it
# using the `state` as `this`. The function will modify the
# `state` in place.
fn = Function(js_code_str)
fn.call(state, js_code_str)

# Turn the code string into an actual function, and call it
# using the `state` as `this`. The function will modify the
# `state` in place.
fn = Function(js_code_str)
fn.call(state, js_code_str)
# Reassign the values of the variables using their maybe new
# values from the `state`.
@_updateVariablesFrom(state)

# Reassign the values of the variables using their maybe new
# values from the `state`.
@_updateVariablesFrom(state)
window.document.getElementById('_compile_error_msg')?.innerHTML = ''
catch err
window.document.getElementById('_compile_error_msg')?.innerHTML = err
@_is_executing = false


Expand Down
82 changes: 47 additions & 35 deletions source/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,43 @@
Browser-specific functionality.
###

$ = require 'jquery'
_ = require 'underscore'
_s = require 'underscore.string'

ActiveMarkdown = require './ActiveMarkdown'
Controls = require './Controls'
Executor = require './Executor'
DragManager = require './DragManager'

ActiveCodeBlock = require './elements/ActiveCodeBlock'
ChartElement = require './elements/ChartElement'
RangeElement = require './elements/RangeElement'
StringElement = require './elements/StringElement'
SwitchElement = require './elements/SwitchElement'


# TODO: Have these listen to events from the elements, instead of needing
# to be global.
window.executor = new Executor()
window.drag_manager = new DragManager()

ActiveMarkdown.makeActive = (options) ->
ActiveMarkdown.options = options

controls = new Controls
el : $('#AMControls')
collapsed_code : options.collapsed_code
filename : options.filename
# controls = new Controls
# el : $('#AMControls')
# collapsed_code : options.collapsed_code
# filename : options.filename

window.executor._code_blocks = []

list_of_unused_vars = []
for v of window.executor._variables
if window.executor._variables.hasOwnProperty v
list_of_unused_vars.push v

$('pre').each (i, el) ->
$el = $(el)
$code = $el.find('code')
if not $code.attr('class')
source = $code.text()
$new_el = $('<div>')
$new_el.attr('id', 'AMDEditorElement_' + i)
$el.replaceWith($new_el)
executor.addCodeBlock new ActiveCodeBlock
window.executor.addCodeBlock new ActiveCodeBlock
el : $new_el
source : source

Expand All @@ -58,37 +59,48 @@ ActiveMarkdown.makeActive = (options) ->

if element_class?
element_class.make($el, config_str)
var_name = config_str.match(/[^:]*/)[0]
i = list_of_unused_vars.indexOf var_name
if (i > -1)
list_of_unused_vars.splice i, 1
else
console.error 'Unable to make element for', $el
if options.debug
#console.error 'Unable to make element for', $el
#if options.debug
if $el.context.innerHTML is '' then $el.context.innerHTML = 'error'
$el.addClass('error')
$el.append """
<span class="error-feedback">
Unable to make element:<br><span class="config-string">{#{ config_str }}</span>
</span>
"""
for v in list_of_unused_vars
delete window.executor._variables[v]


# Unescape the raw source.
$('#AMRaw').text(unescape($('#AMRaw').text()))
window.executor._deferredExecute()


# Add section links to each heading, updating the ids with a counter if
# necessary to ensure each one is unique.
heading_counts = {}
$('h1, h2, h3, h4, h5, h6').each (i, el) ->
$el = $(el)

id = _s.slugify(_s.words(el.innerText).join('-'))
heading_counts[id] ?= 0
heading_counts[id] += 1

if heading_counts[id] > 1
id = "#{id}-#{heading_counts[id]}"

el.id = id
$el.prepend """
<a class="section-link" href="##{id}">#</a>
"""

window.ActiveMarkdown = ActiveMarkdown
# TODO make anchor id's and update href attr instead prepend
# ActiveMarkdown.makeHeadingSectionLinks = () ->
# heading_counts = {}
# $('h1, h2, h3, h4, h5, h6').each (i, el) ->
# $el = $(el)
# console.log $el
# id = _s.slugify(_s.words(el.innerText).join('-'))
# heading_counts[id] ?= 0
# heading_counts[id] += 1

# if heading_counts[id] > 1
# id = "#{id}-#{heading_counts[id]}"

# el.id = id
# $el.prepend """
# <a class="section-link" href="##{id}">#</a>
# """

exportActiveMarkdown = () ->
window.executor = new Executor()
window.ActiveMarkdown = ActiveMarkdown

module.exports = exportActiveMarkdown
21 changes: 14 additions & 7 deletions source/elements/ActiveCodeBlock.coffee
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
NamedView = require '../libraries/NamedView'
Ace = require 'brace'

require('brace/mode/coffee')
#require('brace/theme/monokai')

class ActiveCodeBlock extends NamedView
@_name: 'ActiveCodeBlock'

initialize: ({ source }) ->
@_source = source
window.ace_editors = {} if !window.ace_editors
window.ace_editors[@el.id] = Ace.edit @el if !window.ace_editors[@el.id]
@_editor = window.ace_editors[@el.id]
@el.parentElement.insertBefore(@_editor.container, @el)
@render()

render: ->
@_editor = CodeMirror @el,
value : @_source
mode : 'coffeescript'
lineNumbers : true
viewportMargin : Infinity
theme : 'solarized'

@_editor.$blockScrolling = Infinity
@_editor.setValue(@_source)
@_editor.clearSelection()
#@_editor.setTheme('ace/theme/monokai')
@_editor.getSession().setMode('ace/mode/coffee')
@_editor.setOptions maxLines: Infinity
@_editor.on('blur', @_update)

_update: =>
Expand Down
4 changes: 3 additions & 1 deletion source/elements/RangeElement.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BaseElement = require './BaseElement'
DragManager = require '../DragManager'

{
parseNumber
Expand Down Expand Up @@ -45,6 +46,7 @@ class RangeElement extends BaseElement
///

initialize: (parsed_config) ->
@drag_manager = new DragManager()
parsed_config.value = @_parseTextContent(parsed_config)
delete parsed_config.text_content
@model = executor.getOrCreateVariable(parsed_config)
Expand Down Expand Up @@ -166,7 +168,7 @@ class RangeElement extends BaseElement


_startDragging: (e) ->
drag_manager.start(e, this, 'x')
@drag_manager.start(e, this, 'x')
@_original_value = @model.get('value')
@$el.addClass('active')
e.preventDefault()
Expand Down
4 changes: 2 additions & 2 deletions source/elements/StringElement.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class StringElement extends BaseElement
"""

initialize: (parsed_config) ->
@_text_content = parsed_config.text_content
@model = executor.getOrCreateVariable
name: parsed_config.name
@model.on('change:value', @render)
value: parsed_config.text_content
@model.on('change', @render)
@_fadeChange = _.debounce(@_doFade, 1000)


Expand Down
3 changes: 2 additions & 1 deletion source/elements/SwitchElement.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SwitchElement extends BaseElement
parsed_config.value = @_parseTextContent(parsed_config)
delete parsed_config.text_content
@model = executor.getOrCreateVariable(parsed_config)
@model.on('change:value', @render)

@_parseConfig: (config_match) ->
###
Expand Down Expand Up @@ -59,7 +60,7 @@ class SwitchElement extends BaseElement
group = text_content.match(pattern)
return group

default_value = undefined
default_value = false
@_before_text = ''
@_after_text = ''
@_text_content = text_content
Expand Down
Loading