Skip to content

Commit

Permalink
Extend the enter-on-empty-line behavior to work with any format with …
Browse files Browse the repository at this point in the history
…'inherit: true'
  • Loading branch information
thomsbg authored and Réal Provencher committed Mar 11, 2016
1 parent ebd8dbe commit 59182fc
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,29 @@ class Keyboard
[line, offset] = @quill.editor.doc.findLineAt(range.start)
[leaf, offset] = line.findLeafAt(offset)
delta = new Delta().retain(range.start)

# if on an empty line formatted as a list, remove the format
if range.isCollapsed() and line.length == 1 and (line.formats.bullet or line.formats.list)
delta.retain(1, { list: false, bullet: false })
removeInheritedFormats = _.reduce(line.formats, (formats, value, name) =>
format = @quill.editor.doc.formats[name]
if format and format.isType('line') and format.config.inherit
formats[name] = false
return formats
, {})
removeNonInheritedFormats = _.reduce(line.formats, (formats, value, name) =>
format = @quill.editor.doc.formats[name]
if format and format.isType('line') and !format.config.inherit
formats[name] = false
return formats
, {})

# if on an empty line, remove the inheritable formats
if range.isCollapsed() and line.length == 1 and Object.keys(removeInheritedFormats).length > 0
delta.retain(1, removeInheritedFormats)
else
delta.insert('\n', line.formats).delete(range.end - range.start)

# if creating a new empty line (was at the end of the old line),
# remove line formats from the new line that should not be inherited
if !leaf.next and offset == leaf.length
delta.retain(1, _.reduce(line.formats, (formats, value, name) =>
format = @quill.editor.doc.formats[name]
if format and format.isType('line') and !format.config.inherit
formats[name] = false
return formats
, {}))
delta.retain(1, removeNonInheritedFormats)

@quill.updateContents(delta, Quill.sources.USER)
_.each(leaf.formats, (value, format) =>
Expand Down

0 comments on commit 59182fc

Please sign in to comment.