Skip to content

Commit

Permalink
Merge library displayName and id into one 'name' property
Browse files Browse the repository at this point in the history
  • Loading branch information
towerofnix committed Aug 19, 2017
1 parent 5da8c1f commit 6547972
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
36 changes: 13 additions & 23 deletions src/Scratch.as
Original file line number Diff line number Diff line change
Expand Up @@ -1664,13 +1664,8 @@ package {
return;
}

if (!('id' in library)) {
DialogBox.notify("Cannot Import", "No 'id' property in library object.");
return;
}

if (!('displayName' in library)) {
DialogBox.notify("Cannot Import", "No 'displayName' property in library object.");
if (!('name' in library)) {
DialogBox.notify("Cannot Import", "No 'name' property in library object.");
return;
}

Expand Down Expand Up @@ -1701,8 +1696,8 @@ package {
public function removeLibraryByPrefixString(targetObj:ScratchObj, prefix:String):void {
// Removes a block library.
// That just means we need to remove all custom blocks whose specs begin
// with the "magic prefix" (which is determined by the library's "id" and
// "displayName" properties).
// with the "magic prefix" (which is determined by the library's "name"
// property).
var newScripts:Array = [];
for each (var script:Block in targetObj.scripts) {
if (script.op === Specs.PROCEDURE_DEF && script.spec.indexOf(prefix) === 0) {
Expand All @@ -1713,7 +1708,6 @@ package {
targetObj.scripts = newScripts;
}


public function exportLibraryOfBlock(block:Block):void {
// This only knows to assume that the targetObj is the editor-selected
// object, which is Not Good.. It's impossible to know who owns a block,
Expand All @@ -1730,17 +1724,14 @@ package {
return;
}

var library:Object = {
id: match.libraryID,
displayName: match.displayName
};
var library:Object = {name: match.name};

var targetObj:ScratchObj = viewedObj();

exportLibraryScriptsOf(targetObj, library);

var jsonData = util.JSON.stringify(library);
var defaultName:String = library.id + ".json";
var defaultName:String = library.name + ".json";
var file:FileReference = new FileReference();
file.addEventListener(Event.COMPLETE, success);
file.save(jsonData, defaultName);
Expand Down Expand Up @@ -1778,16 +1769,16 @@ package {
}

public function getLibraryPrefixString(library:Object):String {
return "$$" + library.id + "$" + library.displayName + ":";
return "$$" + library.name + ":";
}

public function parseLibraryPrefixString(str:String):Object {
// Reads a library-prefix string. Note that the given string can also
// include text past the prefix (so "$$foo$Foo Library:baaaz)" works just
// as well as "$$foo$Foo Library:" alone).
// include text past the prefix (so "$$Foo Library:baaaz)" works just
// as well as "$$Foo Library:" alone).

// In human terms: "$$" <library id> "$" <display name> ":"
var re:RegExp = /^\$\$([^$]+)\$([^:]+):/;
// In human terms: "$$" <library name> ":"
var re:RegExp = /^\$\$([^$]+):/;
var match = re.exec(str);

if (match == null) {
Expand All @@ -1798,11 +1789,10 @@ package {
var rest:String = str.slice(prefix.length);

return {
libraryID: match[1],
displayName: match[2],
name: match[1],
prefix: prefix,
rest: rest,
displayText: "(" + match[2] + ") " + rest
displayText: "(" + match[1] + ") " + rest
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/scratch/BlockMenus.as
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ package scratch {

var match:Object = app.parseLibraryPrefixString(block.spec);
if (match !== null) {
m.addItem("export library " + match.displayName, exportLibrary);
m.addItem("remove library " + match.displayName, removeLibrary);
m.addItem("export library " + match.name, exportLibrary);
m.addItem("remove library " + match.name, removeLibrary);
}

showMenu(m);
Expand Down
3 changes: 1 addition & 2 deletions src/scratch/PaletteBuilder.as
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ public class PaletteBuilder {
var targetObj:ScratchObj = app.viewedObj();

app.importLibraryTo(targetObj, {
id: "scrap",
displayName: "Scrap",
name: "Scrap",
scripts: [
[
["procDef", "set broadcast %m.broadcast 's var %m.broadcastVar to %s", ["bc", "var", "value"], ["", "", ""], false],
Expand Down

0 comments on commit 6547972

Please sign in to comment.