Skip to content

Instantly share code, notes, and snippets.

@joeldrapper
Created September 26, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeldrapper/4744885b63d29107e8f516cde8031b7f to your computer and use it in GitHub Desktop.
Save joeldrapper/4744885b63d29107e8f516cde8031b7f to your computer and use it in GitHub Desktop.
Trix stuff
class @TrixFormatter extends @BaseObject
constructor: (editor) ->
@editor = editor
format: ->
@position = @editor.getPosition()
@text = @editor.getDocument().toString()
@before = @text.substr 0, @position
@after = @text.substr @position, @text.length
@character = @text.charAt(@position - 1)
unless @editor.attributeIsActive("heading1") or @editor.attributeIsActive("code") or @editor.attributeIsActive("quote")
# Heading
if @before.match /^# $/m
@editor.setSelectedRange [@position - 2, @position]
@editor.recordUndoEntry "before automatically inserting a heading"
@editor.deleteInDirection "backward"
@editor.activateAttribute "heading1"
# Bullet List
else if @before.match /^(\-|\*|\.) $/m
@editor.setSelectedRange [@position - 2, @position]
@editor.recordUndoEntry "before automatically inserting a bullet list"
@editor.deleteInDirection "backward"
@editor.activateAttribute "bullet"
# Numbered List
else if @before.match /^\d\. $/m
@editor.setSelectedRange [@position - 3, @position]
@editor.recordUndoEntry "before automatically inserting a numbered list"
@editor.deleteInDirection "backward"
@editor.activateAttribute "number"
# Quote
else if @before.match /^\> $/m
@editor.setSelectedRange [@position - 2, @position]
@editor.recordUndoEntry "before automatically inserting a block quote"
@editor.deleteInDirection "backward"
@editor.activateAttribute "quote"
# Code
else if @before.match /^``` $/m
@editor.setSelectedRange [@position - 4, @position]
@editor.recordUndoEntry "before automatically inserting a code block"
@editor.deleteInDirection "backward"
@editor.activateAttribute "code"
smartPunctuation: ->
unless @editor.attributeIsActive "code"
# Double quote as first character
if @position is 1 and @character is '"'
@editor.deleteInDirection "backward"
@editor.insertString "“"
# Single quote as first character
else if @position is 1 and @character is "'"
@editor.deleteInDirection "backward"
@editor.insertString "‘"
# Double quote after white space
else if @before.match /\s"$/
@editor.deleteInDirection "backward"
@editor.insertString "“"
# Single quote after white space
else if @before.match /\s'$/
@editor.deleteInDirection "backward"
@editor.insertString "‘"
# Double quote while in an open double quote span
else if @before.match(/“[^”]*"$/) and not @after.match(/^[^“]*”/)
@editor.deleteInDirection "backward"
@editor.insertString "”"
# Single quote while in an open single quote span
else if @before.match(/‘[^’]*'$/) and not @after.match(/^[^‘]*’/)
@editor.deleteInDirection "backward"
@editor.insertString "’"
# Double quote after a digit or fraction
else if @before.match /[0-9↉½⅓¼⅕⅙⅐⅛⅑⅒⅔⅖¾⅗⅜⅘⅚⅝⅞]"$/
@editor.deleteInDirection "backward"
@editor.insertString '″'
# Single quote after a digit or fraction but not right before an s
else if @before.match(/[0-9↉½⅓¼⅕⅙⅐⅛⅑⅒⅔⅖¾⅗⅜⅘⅚⅝⅞]'$/) and not @after.match(/^s/)
@editor.deleteInDirection "backward"
@editor.insertString "′"
# Apostrophe followed by an s
else if @before.match /′s$/
currentPosition = @position
@editor.setSelectedRange @position - 1
@editor.deleteInDirection "backward"
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Anything other than a whitespace followed by a single quote
else if @before.match /[^\s]'$/
@editor.deleteInDirection "backward"
@editor.insertString "’"
# Open small quote followed by an "em", "cause", "n", or "twas"
else if match = @before.match /‘(em|cause|n|twas)[^a-z0-9]/i
currentPosition = @position
@editor.setSelectedRange [
@position - 2 - match[1].length,
@position - 1 - match[1].length
]
@editor.insertString "’"
@editor.setSelectedRange currentPosition
else if @before.match /‘[0-9]{2}[^a-rt-z0-9]$/i
currentPosition = @position
@editor.setSelectedRange [@position - 4, @position - 3]
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Special cases for iPhone auto-correct
else if match = @before.match /'(t|ll|re|m|ve|d|s)$/i
currentPosition = @position
@editor.setSelectedRange currentPosition - match[1].length
@editor.deleteInDirection "backward"
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Ellipses
else if @before.match /\.\.\.$/i
@editor.setSelectedRange [@position - 3, @position]
@editor.recordUndoEntry "before automatically inserting an ellipses"
@editor.deleteInDirection "backward"
@editor.insertString "…"
# Catch a double space
else if match = @before.match /\ (\ +)$/i
@editor.setSelectedRange [@position - 1 - match[1].length, @position]
@editor.deleteInDirection "backward"
@editor.insertString ". "
# Fix macOS doube space full stop insertion
else if @before.match /\ \.\ $/i
@editor.setSelectedRange [@position - 3, @position]
@editor.deleteInDirection "backward"
@editor.insertString ". "
class @TrixPunctuator extends @BaseObject
constructor: (editor) ->
@editor = editor
punctuate: ->
@position = @editor.getPosition()
@text = @editor.getDocument().toString()
@before = @text.substr 0, @position
@after = @text.substr @position, @text.length
@character = @text.charAt(@position - 1)
unless @editor.attributeIsActive("code") or @editor.canRedo()
switch
# Double-quote as first character
when @before.match /^"$/
@editor.deleteInDirection "backward"
@editor.insertString "“"
# Single quote as first character
when @position is 1 and @character is "'"
@editor.deleteInDirection "backward"
@editor.insertString "‘"
# Double quote after white space
when @before.match /\s"$/
@editor.deleteInDirection "backward"
@editor.insertString "“"
# Single quote after white space
when @before.match /\s'$/
@editor.deleteInDirection "backward"
@editor.insertString "‘"
# Double quote while in an open double quote span
when @before.match(/“[^”]*"$/) and not @after.match(/^[^“]*”/)
@editor.deleteInDirection "backward"
@editor.insertString "”"
# Single quote while in an open single quote span
when @before.match(/‘[^’]*'$/) and not @after.match(/^[^‘]*’/)
@editor.deleteInDirection "backward"
@editor.insertString "’"
# Double quote after a digit or fraction
when @before.match /[0-9↉½⅓¼⅕⅙⅐⅛⅑⅒⅔⅖¾⅗⅜⅘⅚⅝⅞]"$/
@editor.deleteInDirection "backward"
@editor.insertString '″'
# Single quote after a digit or fraction but not right before an s
when @before.match(/[0-9↉½⅓¼⅕⅙⅐⅛⅑⅒⅔⅖¾⅗⅜⅘⅚⅝⅞]'$/) and not @after.match(/^s/)
@editor.deleteInDirection "backward"
@editor.insertString "′"
# Apostrophe followed by an s
when @before.match /′s$/
currentPosition = @position
@editor.setSelectedRange @position - 1
@editor.deleteInDirection "backward"
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Anything other than a whitespace followed by a single quote
when @before.match /[^\s]'$/
@editor.deleteInDirection "backward"
@editor.insertString "’"
# Open small quote followed by an "em", "cause", "n", or "twas"
when match = @before.match /‘(em|cause|n|twas)[^a-z0-9]/i
currentPosition = @position
@editor.setSelectedRange [
@position - 2 - match[1].length,
@position - 1 - match[1].length
]
@editor.insertString "’"
@editor.setSelectedRange currentPosition
when @before.match /‘[0-9]{2}[^a-rt-z0-9]$/i
currentPosition = @position
@editor.setSelectedRange [@position - 4, @position - 3]
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Special cases for iPhone auto-correct
when match = @before.match /'(t|ll|re|m|ve|d|s)$/i
currentPosition = @position
@editor.setSelectedRange currentPosition - match[1].length
@editor.deleteInDirection "backward"
@editor.insertString "’"
@editor.setSelectedRange currentPosition
# Ellipses
when @before.match /\.\.\.$/i
@editor.setSelectedRange [@position - 3, @position]
@editor.recordUndoEntry "before automatically inserting an ellipses"
@editor.deleteInDirection "backward"
@editor.insertString "…"
# Catch a double space
when match = @before.match /\ (\ +)$/i
@editor.setSelectedRange [@position - 1 - match[1].length, @position]
@editor.deleteInDirection "backward"
@editor.insertString ". "
# Fix macOS doube space full stop insertion
when @before.match /\ \.\ $/i
@editor.setSelectedRange [@position - 3, @position]
@editor.deleteInDirection "backward"
@editor.insertString ". "
when @before.match /[,;]\n+$/i
@editor.setSelectedRange [@position - 1, @position]
@editor.deleteInDirection "backward"
@editor.insertHTML "<br>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment