Skip to content

Commit

Permalink
Custom behavior for the enter key + list formats
Browse files Browse the repository at this point in the history
  • Loading branch information
thomsbg authored and Réal Provencher committed Mar 11, 2016
1 parent 5273169 commit ffad69b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/core/format.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ class Format
bullet:
type: Format.types.LINE
exclude: 'list'
inherit: true
parentTag: 'UL'
tag: 'LI'

list:
type: Format.types.LINE
exclude: 'bullet'
inherit: true
parentTag: 'OL'
tag: 'LI'

Expand Down
17 changes: 16 additions & 1 deletion src/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,22 @@ class Keyboard
return true unless range?
[line, offset] = @quill.editor.doc.findLineAt(range.start)
[leaf, offset] = line.findLeafAt(offset)
delta = new Delta().retain(range.start).insert('\n', line.formats).delete(range.end - range.start)
delta = new Delta().retain(range.start)

# hitting enter on an empty line formatted as a list should 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 })
else
delta.insert('\n', line.formats).delete(range.end - range.start)

# remove line formats from the new line that should not be inherited
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
, {}))

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

0 comments on commit ffad69b

Please sign in to comment.