This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Text expansion shortcut #1057
Merged
Merged
Text expansion shortcut #1057
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
045af44
Merge branch 'master' into text-expansion-shortcut
tangollama d097f69
ignore electron-out
tangollama c0ff406
Checking in the text replacement feature
tangollama a3c0302
aslant reconciliation
tangollama 3c4ffa0
adjustments to shortcakes
tangollama ce721aa
Fixing the underscores in the language translation
tangollama d387979
Merge branch 'master' into text-expansion-shortcut
jkleinsc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Ember from 'ember'; | ||
import EmberValidations from 'ember-validations'; | ||
|
||
export default Ember.Controller.extend(EmberValidations, { | ||
hideCancelButton: true, | ||
updateCapability: 'update_config', | ||
|
||
createExpansion: function() { | ||
let newExpansion = this.get('store').createRecord('text-expansion'); | ||
this.set('newExpansion', newExpansion); | ||
}.on('init'), | ||
|
||
actions: { | ||
cancelExpansion() { | ||
this.createExpansion(); | ||
} | ||
}, | ||
|
||
validations: { | ||
'newExpansion.from': { | ||
presence: true | ||
}, | ||
'newExpansion.to': { | ||
presence: true | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import AbstractIndexRoute from 'hospitalrun/routes/abstract-index-route'; | ||
import { translationMacro as t } from 'ember-i18n'; | ||
|
||
export default AbstractIndexRoute.extend({ | ||
pageTitle: t('admin.text_replacements.page_title'), | ||
hideNewButton: true, | ||
|
||
model() { | ||
let store = this.get('store'); | ||
return store.findAll('text-expansion').then((result) => { | ||
return result.filter((model) => { | ||
let isNew = model.get('isNew'); | ||
console.log(`${model.get('from')} ${isNew}`); | ||
return !isNew; | ||
}); | ||
}); | ||
}, | ||
|
||
setupController(controller, model) { | ||
this._super(controller, model); | ||
controller.createExpansion(); | ||
}, | ||
|
||
actions: { | ||
addExpansion(newExpansion) { | ||
newExpansion.save() | ||
.then(() => { | ||
this.refresh(); | ||
}) | ||
.catch(() => { | ||
this.refresh(); | ||
}); | ||
}, | ||
|
||
deleteExpansion(expansion) { | ||
expansion.deleteRecord(); | ||
expansion.save() | ||
.then(() => { | ||
this.refresh(); | ||
}) | ||
.catch(() => { | ||
this.refresh(); | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<div class="panel panel-primary"> | ||
<div class="panel-body"> | ||
<p>{{t 'admin.text_replacements.repl_desc'}}</p> | ||
<table class="table"> | ||
<tr class="table-header"> | ||
<th>{{t 'labels.from'}}</th> | ||
<th>{{t 'labels.to'}}</th> | ||
<th/> | ||
</tr> | ||
<tbody> | ||
{{#each model as |expansion|}} | ||
<tr> | ||
<td>#{{expansion.from}}</td> | ||
<td>{{expansion.to}}</td> | ||
<td> | ||
<button class="pull-right btn button-default on-white" {{action 'deleteExpansion' expansion}}>{{t 'buttons.delete'}}</button> | ||
</td> | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
<div class="panel"> | ||
<div class="panel-body"> | ||
<h3>{{t 'admin.text_replacements.create_new'}}</h3> | ||
{{#em-form model=newExpansion action="addExpansion" formLayout="horizontal" showErrorsOnFocusIn="true" submitButton=false}} | ||
{{em-input property="from" label=(t 'labels.from') placeholder=(t 'admin.text_replacements.to_replace')}} | ||
{{em-input property="to" label=(t 'labels.from') placeholder=(t 'admin.text_replacements.replace_with')}} | ||
{{/em-form}} | ||
</div> | ||
<div class="panel-footer"> | ||
<button class="btn button-primary on-white" disabled={{isInvalid}} {{action 'addExpansion' newExpansion}}>{{t 'buttons.add'}}</button> | ||
<button class="btn button-default on-white" {{action 'cancelExpansion'}}>{{t 'buttons.cancel'}}</button> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import Ember from 'ember'; | ||
import textExpansion from '../utils/text-expansion'; | ||
|
||
export default Ember.Component.extend({ | ||
i18n: Ember.inject.service(), | ||
store: Ember.inject.service(), | ||
|
||
userText: '', | ||
|
||
didInsertElement() { | ||
try { | ||
let feedbackDiv = document.createElement('div'); | ||
feedbackDiv.style.position = 'absolute'; | ||
// let textarea = this.$()[0].getElementsByTagName('textarea')[0]; | ||
let [textarea] = this.$('textarea'); | ||
this.set('textarea', textarea); | ||
let textPos = textarea.getBoundingClientRect(); | ||
let fbStyle = feedbackDiv.style; | ||
fbStyle.top = `${textPos.bottom}px`; | ||
fbStyle.left = `${textPos.left}px`; | ||
fbStyle.width = `${textarea.offsetWidth}px`; | ||
fbStyle.backgroundColor = 'lightyellow'; | ||
fbStyle.borderStyle = 'solid'; | ||
fbStyle.borderWidth = '1px'; | ||
fbStyle.borderRadius = '3px'; | ||
fbStyle.paddingLeft = '5px'; | ||
fbStyle.visibility = 'hidden'; | ||
|
||
this.set('feedbackDiv', feedbackDiv); | ||
this.get('feedbackText'); | ||
this.get('activeExpansionSite'); | ||
|
||
this.get('store') | ||
.findAll('text-expansion') | ||
.then((expansions) => { | ||
return expansions.reduce((prev, curr) => { | ||
// console.log(`curr ${JSON.stringify(prev)}`); | ||
prev[curr.get('from')] = curr.get('to'); | ||
return prev; | ||
}, {}); | ||
}) | ||
.then((expansions) => { | ||
this.set('expansions', expansions); | ||
}); | ||
|
||
} catch(e) { | ||
// console.log(`didInsert {e}`); | ||
} | ||
}, | ||
|
||
keyUp(k) { | ||
let textArea = k.target; | ||
let text = textArea.value; | ||
this.set('userText', text); | ||
this.set('cursorLocation', textArea.selectionStart); | ||
}, | ||
|
||
keyDown(k) { | ||
if (k.keyCode === 13) { | ||
let possibleSwaps = this.get('possibleSwaps'); | ||
if (possibleSwaps && possibleSwaps.length === 1) { | ||
let swapTo = possibleSwaps[0].to; | ||
let activeSite = this.get('activeExpansionSite'); | ||
let sliceLength = activeSite.match.length; | ||
let currentText = k.target.value; | ||
let modifiedText = currentText.slice(0, activeSite.index) + swapTo + currentText.slice(activeSite.index + sliceLength); | ||
k.target.value = modifiedText; | ||
|
||
k.preventDefault(); | ||
k.returnValue = false; | ||
k.cancelBubble = true; | ||
return false; | ||
} | ||
} | ||
}, | ||
|
||
// Find an expandable word that has the cursor within it | ||
activeExpansionSite: Ember.computed('userText', 'cursorLocation', function() { | ||
|
||
let userText = this.get('userText'); | ||
let textarea = this.get('textarea'); | ||
if (!textarea) { | ||
return null; | ||
} | ||
let cursorLoc = textarea.selectionStart; | ||
let subjects = textExpansion.findExpansionSubjects(userText); | ||
let sites = textExpansion.findExpansionSites(userText, subjects); | ||
|
||
return sites.find((s) => { | ||
let endIndex = s.index + s.match.length; | ||
|
||
return cursorLoc >= s.index && cursorLoc <= endIndex; | ||
}); | ||
}), | ||
|
||
// If an expansion site is active, which possible swaps could occur there? | ||
possibleSwaps: Ember.computed('activeExpansionSite', 'expansions', function() { | ||
let activeSite = this.get('activeExpansionSite'); | ||
|
||
if (activeSite) { | ||
let expansions = this.get('expansions'); | ||
return Object.keys(expansions) | ||
.filter((ex) => { | ||
return ex.startsWith(activeSite.term); | ||
}) | ||
.sort() | ||
.map((from) => { | ||
return { | ||
from, | ||
to: expansions[from] | ||
}; | ||
}); | ||
} | ||
}), | ||
|
||
expansionText: Ember.computed('possibleSwaps', 'activeExpansionSite', 'userText', function() { | ||
let result = ''; | ||
|
||
let i18n = this.get('i18n'); | ||
let possibleSwaps = this.get('possibleSwaps'); | ||
if (possibleSwaps) { | ||
let activeSite = this.get('activeExpansionSite'); | ||
|
||
if (possibleSwaps.length === 1) { | ||
let swapTo = possibleSwaps[0].to; | ||
result = i18n.t('admin.text_replacements.perform_expand', { from: activeSite.term, to: swapTo }); | ||
} else if (possibleSwaps.length > 1) { | ||
let possible = possibleSwaps | ||
.map((swap) => { | ||
return swap.from; | ||
}).join(', '); | ||
result = i18n.t('admin.text_replacements.possible_expansions', { possible }); | ||
} else { | ||
result = i18n.t('admin.text_replacements.no_matches', { term: activeSite.term }); | ||
} | ||
} | ||
|
||
return result; | ||
}), | ||
|
||
expansionDivStyle: Ember.computed('expansionText', function() { | ||
let expansionText = this.get('expansionText'); | ||
let visiblility = expansionText ? 'visible' : 'hidden'; | ||
let textArea = this.get('textarea'); | ||
|
||
let styleString = `visibility: ${visiblility};`; | ||
|
||
if (textArea) { | ||
let textPos = textArea.getBoundingClientRect(); | ||
styleString += ` top: ${textPos.bottom}px; left: ${textPos.left}px; width: ${textArea.offsetWidth}px;`; | ||
} | ||
return new Ember.Handlebars.SafeString(styleString); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tangollama these need to be camel cased.