Skip to content

Commit

Permalink
Fixed issues with inserting figure blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
leahfitch committed Mar 17, 2016
1 parent 647d39b commit e7827ef
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 33 deletions.
92 changes: 67 additions & 25 deletions baseline/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,88 @@ var Editor = function Editor(options)
this.onselectionchange = options.onselectionchange
this.dom_window = options.dom_window || window
this.dom_document = this.dom_window.document

this.container = options.container
this.container.contentEditable = true
this.container.addEventListener('keydown', this.keydown_handler.bind(this))
this.container.addEventListener('keypress', this.keypress_handler.bind(this))
this.container.addEventListener('keyup', this.keyup_handler.bind(this))
this.dom_document.addEventListener('copy', this.copy_handler.bind(this))
this.container.addEventListener('paste', this.paste_handler.bind(this))
this.container.addEventListener('click', this.click_handler.bind(this))
this.dom_document.addEventListener('selectionchange', this.selectionchange_handler.bind(this))
this.selection_changed = false
this.ignore_selection_change = false

this.bound_event_listeners = {
keydown: this.keydown_handler.bind(this),
keypress: this.keypress_handler.bind(this),
keyup: this.keyup_handler.bind(this),
copy: this.copy_handler.bind(this),
paste: this.paste_handler.bind(this),
click: this.click_handler.bind(this),
selectionchange: this.selectionchange_handler.bind(this)
}
this.container_events = ['keydown', 'keypress', 'keyup', 'paste', 'click']
this.document_events = ['copy', 'selectionchange']

this.parser = new Parser({
block_recognizers: defaults.block_recognizers,
annotation_types: defaults.annotation_types
})

this.commands = {}
Object.keys(defaults.commands).forEach(function (k)
{
this.commands[k] = defaults.commands[k]
}.bind(this))

this.set_container(options.container)
}

Editor.prototype.set_container = function (container)
{
if (this.container)
{
this.remove_event_listeners()
}

this.container = container
this.container.contentEditable = true

this.add_event_listeners()

this.renderer = new Renderer({
container: this.container,
onblockchange: this.onblockchange.bind(this),
document: this.dom_document,
parser: this.parser
})

if (options.document)
{
this.document = options.document
}
else
{
this.document = new Document({
blocks: this.parser.parse_dom(this.container)
})
}
this.document = new Document({
blocks: this.parser.parse_dom(this.container)
})

this.changes = new ChangeTracker(this.document)

this.commands = {}
Object.keys(defaults.commands).forEach(function (k)
this.update_range_from_window()
this.render()
}

Editor.prototype.add_event_listeners = function ()
{
this.container_events.forEach(function (k)
{
this.commands[k] = defaults.commands[k]
this.container.addEventListener(k, this.bound_event_listeners[k])
}.bind(this))

this.update_range_from_window()
this.render()
this.document_events.forEach(function (k)
{
this.dom_document.addEventListener(k, this.bound_event_listeners[k])
}.bind(this))
}

Editor.prototype.remove_event_listeners = function ()
{
this.container_events.forEach(function (k)
{
this.container.removeEventListener(k, this.bound_event_listeners[k])
}.bind(this))

this.document_events.forEach(function (k)
{
this.dom_document.removeEventListener(k, this.bound_event_listeners[k])
}.bind(this))
}

Editor.prototype.render = function ()
Expand Down Expand Up @@ -224,6 +261,11 @@ Editor.prototype.keyup_handler = function (evt)

Editor.prototype.selectionchange_handler = function (evt)
{
if (this.ignore_selection_change)
{
return
}

this.selection_changed = true
this.update_range_from_window()

Expand Down
9 changes: 8 additions & 1 deletion baseline/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ Model.extend = function (parent, props)
new_props[k] = props[k]
})

return Model(new_props)
var model = Model(new_props)
model._parent = parent
return model
}

Model.is_instance = function (inst, model)
{
var child = inst.constructor

if (child === model)
{
return true
}

while (child._parent)
{
if (child._parent === model)
Expand Down
15 changes: 11 additions & 4 deletions baseline/annotations/AnnotationTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = Model(
return this.set(results)
},

filter: function ()
filter: function (fn)
{
var results = []
this.root.walk(function (ann)
Expand All @@ -65,12 +65,19 @@ module.exports = Model(
return this.set(results)
},

in_range: function (offset, length)
in_range: function (start, end)
{
return this.filter(function (ann)
var offset = start
var length = end - start
var matches = []
this.root.walk(function (ann)
{
return ann.overlaps(offset, length)
if (ann.overlaps(offset, length))
{
matches.push(ann)
}
})
return matches
},

remove: function (start, end)
Expand Down
8 changes: 7 additions & 1 deletion baseline/blocks/FigureBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var FigureBlock = Model.extend(Block,
alt: this.alt,
width: this.width,
height: this.height,
contentEditable: true,
ondragstart: function (e)
{
e.preventDefault()
Expand All @@ -44,7 +45,7 @@ var FigureBlock = Model.extend(Block,
if (this.caption || this.attribution_name || this.attribution_url)
{
return h('figcaption', [
this.caption ? h('p', { className: 'caption' }, this.caption) : null,
this.caption ? h('p', { className: 'caption', contentEditable: true }, this.caption) : null,
this.render_attribution()
])
}
Expand Down Expand Up @@ -75,6 +76,11 @@ var FigureBlock = Model.extend(Block,
offset: 0
})
}
},

append_to: function (block)
{
return [block, this]
}
})

Expand Down
1 change: 1 addition & 0 deletions baseline/commands/insert_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function (editor, new_blocks)
blocks = editor.document.blocks

var result = blocks[range.start.block].insert(range.start, new_blocks)

editor.update_document(
{
blocks: blocks.slice(0, range.start.block)
Expand Down
4 changes: 3 additions & 1 deletion baseline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ module.exports =
AnnotationType: require('./annotations/AnnotationType'),
Annotation: require('./annotations/Annotation'),
SimpleBlock: require('./blocks/SimpleBlock'),
ListBlock: require('./blocks/ListBlock')
ListBlock: require('./blocks/ListBlock'),
FigureBlock: require('./blocks/FigureBlock'),
Model: require('./Model')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "baseline-edit",
"version": "0.1.5",
"version": "0.1.6",
"main": "baseline",
"description": "More saner rich text editing for the web",
"scripts": {
Expand Down

0 comments on commit e7827ef

Please sign in to comment.