Commit 084d2df0 authored by chris's avatar chris

all change cases

parent 972632d3
import { forEach } from 'lodash'
import { Tool, FontAwesomeIcon as Icon, documentHelpers } from 'substance'
import toTitleCase from '../../helpers/titleCase'
class ChangeCaseTool extends Tool {
constructor(...args) {
......@@ -83,38 +83,71 @@ class ChangeCaseTool extends Tool {
}
changeCase(action) {
this.extendState({
open: false,
})
this.toggleDropdown()
const text = this.getSelectionText()
const selection = this.context.editorSession.getSelection()
switch (action) {
case 'uppercase':
this.transformToUpper()
this.transformToUpper(selection, text)
break
case 'lowerase':
this.transformToLower()
this.transformToLower(selection, text)
break
case 'sentencecase':
this.transformToSentence()
this.transformToSentence(selection, text)
break
case 'titlecase':
this.transformToTitle()
this.transformToTitle(selection, text)
break
}
}
transformToUpper() {
console.log('upper')
transformToUpper(selection, text) {
this.context.editorSession.transaction(tx => {
tx.deleteSelection()
tx.insertText(text.toUpperCase())
})
}
transformToLower() {
console.log('lower')
transformToLower(selection, text) {
this.context.editorSession.transaction(tx => {
tx.deleteSelection()
tx.insertText(text.toLowerCase())
})
}
transformToSentence() {
console.log('sentence')
transformToSentence(selection, text) {
if (
text.charAt(0).match(/^[a-z]+$/i) !== null &&
text.charAt(0).match(/^[a-z]+$/i)[0]
) {
this.context.editorSession.transaction(tx => {
tx.deleteSelection()
tx.insertText(text.charAt(0).toUpperCase() + text.slice(1))
})
} else {
this.context.editorSession.transaction(tx => {
tx.deleteSelection()
tx.insertText(text.charAt(0))
tx.insertText(text.charAt(1).toUpperCase() + text.slice(2))
})
}
}
transformToTitle(selection, text) {
this.context.editorSession.transaction(tx => {
tx.deleteSelection()
tx.insertText(text.toTitleCase())
})
}
transformToTitle() {
console.log('title')
getSelectionText() {
let text = ''
if (window.getSelection) {
text = window.getSelection().toString()
} else if (document.selection && document.selection.type != 'Control') {
text = document.selection.createRange().text
}
return text
}
getInitialState() {
......
export default (String.prototype.toTitleCase = function() {
'use strict'
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|v.?|vs.?|via)$/i
var alphanumericPattern = /([A-Za-z0-9\u00C0-\u00FF])/
var wordSeparators = /([ :–—-])/
return this.split(wordSeparators)
.map(function(current, index, array) {
if (
/* Check for small words */
current.search(smallWords) > -1 &&
/* Skip first and last word */
index !== 0 &&
index !== array.length - 1 &&
/* Ignore title end and subtitle start */
array[index - 3] !== ':' &&
array[index + 1] !== ':' &&
/* Ignore small words that start a hyphenated phrase */
(array[index + 1] !== '-' ||
(array[index - 1] === '-' && array[index + 1] === '-'))
) {
return current.toLowerCase()
}
/* Ignore intentional capitalization */
if (current.substr(1).search(/[A-Z]|\../) > -1) {
return current
}
/* Ignore URLs */
if (array[index + 1] === ':' && array[index + 2] !== '') {
return current
}
/* Capitalize the first letter */
return current.replace(alphanumericPattern, function(match) {
return match.toUpperCase()
})
})
.join('')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment