Skip to content

Commit

Permalink
RegEx not working in Sketch 47 #55
Browse files Browse the repository at this point in the history
fix - RegEx not working in Sketch 47 #55
add - Save find and replace text
  • Loading branch information
thierryc committed Nov 22, 2017
1 parent ed3807c commit 7edb05f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var FindAndReplace = function (context) {

var presets, userDefaults, document, selection, textToFind, textToReplace, searchScope, matchRegex, matchRegexStart, matchRegexEnd, matchRegexCase, REPLACE_ALL, READY_TO_SEARCH, CANCELLED, NOT_READY, itemsMatched;
var version = '1.13';
var version = '1.15';

// Initialise
initialise(context);
Expand Down Expand Up @@ -107,11 +107,11 @@ var FindAndReplace = function (context) {

// Find text input
userInterface.addTextLabelWithValue("Find");
userInterface.addTextFieldWithValue(textToFind);
userInterface.addTextFieldWithValue(userDefaults.textToFindOrigine || '');

// Replace with text input
userInterface.addTextLabelWithValue("Replace with");
userInterface.addTextFieldWithValue(textToReplace);
userInterface.addTextFieldWithValue(userDefaults.textToReplaceOrigine || '');

// Scope
userInterface.addTextLabelWithValue("Search scope");
Expand All @@ -136,7 +136,7 @@ var FindAndReplace = function (context) {

// Regex
userInterface.addTextLabelWithValue("RegEx (for expert)")
userInterface.addAccessoryView(createRadioButtons(["Yes", "No"], userDefaults.regex))
userInterface.addAccessoryView(createRadioButtons(["No", "Yes"], userDefaults.regex))

// Replace and cancel buttons
userInterface.addButtonWithTitle('Replace All');
Expand Down Expand Up @@ -211,6 +211,9 @@ var FindAndReplace = function (context) {
textToReplace = modal.viewAtIndex(3).stringValue();
searchScope = [[[modal viewAtIndex: 5] selectedCell] tag];

userDefaults.textToFindOrigine = textToFind;
userDefaults.textToReplaceOrigine = textToReplace;

userDefaults.caseSensitivity = [[[modal viewAtIndex: 7] selectedCell] tag];
userDefaults.caseReplace = [[[modal viewAtIndex: 9] selectedCell] tag];
userDefaults.matchWhere = [[[modal viewAtIndex: 11] selectedCell] tag];
Expand All @@ -223,15 +226,11 @@ var FindAndReplace = function (context) {

// Make sure we have text to find
if (textToFind != "") {

// Yeah, ready to go
result = READY_TO_SEARCH;

} else {

// Need something in find and replace
result = NOT_READY;

}

} else {
Expand All @@ -255,14 +254,13 @@ var FindAndReplace = function (context) {

// Build the regex to match on, based on the user options
var regexString = matchRegexStart[userDefaults.matchWhole][userDefaults.matchWhere] + cleanSearch(textToFind) + matchRegexEnd[userDefaults.matchWhole][userDefaults.matchWhere];
if (!userDefaults.regex) {

if (!userDefaults.regex) { // 0
matchRegex = new RegExp(regexString, matchRegexCase[userDefaults.caseSensitivity]);
} else {
} else { // 1
matchRegex = new RegExp(textToFind, matchRegexCase[userDefaults.caseSensitivity]);
}



// Determine the scope and launch the search accordingly
switch (searchScope) {

Expand Down Expand Up @@ -335,13 +333,13 @@ var FindAndReplace = function (context) {

function replaceValue(sourceString, replaceString) {
var newStringValue;
if (userDefaults.regex == 1) {
if (!userDefaults.regex) { // 0
newStringValue = sourceString.replace(matchRegex,
function(eachMatch){
return doIntelligentReplace(eachMatch, replaceString);
}
);
} else {
} else { // 1
newStringValue = sourceString.replace(matchRegex, replaceString);
}
return newStringValue
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

![Find and Replace for Sketch](https://raw.githubusercontent.com/thierryc/Sketch-Find-And-Replace/master/Ressources/logo-256.png)

# Find and Replace for Sketch

Finds text in selected layer(s) and all layers contained within - and replaces it with different text. Features partial, case sensitive and full-document matching.

Now works with symbol overrides. 🎉

Ready for Sketch 47.

![Find and Replace dialog](./Screenshots/screenshot.png)

## Installation

Download and unzip, then double-click on the `Find and Replace.sketchplugin` file.
Download and unzip, then double-click on the `Find and Replace.sketchplugin` file.
Sketch will copy it to the plugins folder and install it automatically.

### Install with Sketch Runner
### Install with Sketch Runner

With Sketch Runner, just go to the `install` command and search for `Find and Replace`. Runner allows you to manage plugins and do much more to speed up your workflow in Sketch. [Download Runner here](http://www.sketchrunner.com).

Expand Down Expand Up @@ -89,7 +90,7 @@ Get the Regex Power ! ⚡️

### "John Smith" to "Smith John"

Find
Find
```
(\w+)\s+(\w+)
```
Expand All @@ -100,9 +101,9 @@ $2 $1
```

Result: Smith John.


#### Replace all double spaces

#### Replace all double spaces

Find
```
Expand All @@ -111,12 +112,12 @@ Find
```

Replace with
```
```
(one space)
```

Follow me on twitter for more tips.
Follow me on twitter for more tips.

https://twitter.com/Autre_planete

Expand Down
13 changes: 13 additions & 0 deletions appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<link>https://raw.githubusercontent.com/thierryc/Sketch-Find-And-Replace/master/appcast.xml</link>
<description>Finds text in selected layer(s) and all layers contained within - and replaces it with different text. Features partial, case sensitive and full-document matching.</description>
<language>en</language>
<item>
<title>1.15</title>
<description>
<![CDATA[
<ul>
<li>Fix regex for sktech 47</li>
<li>Save find and replace string</li>
</ul>
]]>
</description>
<pubDate>Wed Nov 22 12:00:00 EDT 2017</pubDate>
<enclosure url="https://github.com/thierryc/Sketch-Find-And-Replace/archive/1.14.zip" type="application/octet-stream" sparkle:version="1.15"/>
</item>
<item>
<title>1.14</title>
<description>
Expand Down

0 comments on commit 7edb05f

Please sign in to comment.