Skip to content

Commit

Permalink
feat(asset): fix rename script attribute asset->attribute name: now u…
Browse files Browse the repository at this point in the history
…pdate in all script components
  • Loading branch information
yyc-git committed Apr 11, 2019
1 parent ce19924 commit fee5180
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ let getNodeName = node => getNodeData(node).name;

let getNodeNameByData = (nodeData: scriptAttributeNodeData) => nodeData.name;

let getAttributeByData = (nodeData: scriptAttributeNodeData) =>
nodeData.attribute;

let isScriptAttributeNode = node =>
switch (node) {
| ScriptAttributeNode(_, _) => true
Expand Down
29 changes: 27 additions & 2 deletions src/service/state/engine/script/ScriptEngineService.re
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ let _replaceScriptDataByEntriesMap =
script,
scriptDataEntriesMap
/* |> WonderCommonlib.ImmutableHashMapService.getValidEntries
|> WonderCommonlib.SparseMapType.arrayNotNullableToArrayNullable */
|> WonderCommonlib.SparseMapType.arrayNotNullableToArrayNullable */
|> WonderCommonlib.ImmutableSparseMapService.reduceValid(
(. dataMap, (name, newData)) =>
dataMap |> WonderCommonlib.ImmutableHashMapService.has(name) ?
Expand Down Expand Up @@ -302,4 +302,29 @@ let removeAttributeInAllScriptComponents =
),
},
};
};
};

let getAllScriptsWithAttribute =
(({scriptRecord}: StateDataMainType.state) as engineState) => {
let {scriptAttributeMap}: StateDataMainType.scriptRecord = scriptRecord;

scriptAttributeMap |> WonderCommonlib.ImmutableSparseMapService.getValidKeys;
};

let replaceAttributeInAllScriptComponents =
(
(oldAttributeName, newAttributeName),
newAttribute,
({scriptRecord}: StateDataMainType.state) as engineState,
) =>
getAllScriptsWithAttribute(engineState)
|> WonderCommonlib.ArrayService.reduceOneParam(
(. engineState, script) =>
engineState
|> replaceScriptAttribute(
script,
(oldAttributeName, newAttributeName),
newAttribute,
),
engineState,
);
173 changes: 87 additions & 86 deletions src/service/stateTuple/logic/asset/RenameNodeAssetLogicService.re
Original file line number Diff line number Diff line change
Expand Up @@ -127,103 +127,104 @@ let _scriptEventFunctionNodeFunc =
(result, tree, engineState),
nodeId,
nodeData,
) => {
let (result, newTree, engineState) =
result
|> Result.RelationResult.isSuccess
&& NodeAssetService.isIdEqual(nodeId, targetNodeId) ?
{
let (result, newTree) =
switch (
_checkParentNode(parentFolderNode, name, engineState)
|> Result.RelationResult.handleSuccess(() =>
ScriptEventFunctionNodeNameAssetService.isTreeScriptEventFunctionNodesHasTargetName(
name,
tree,
) ?
Result.RelationResult.fail(
LanguageUtils.getMessageLanguageDataByType(
"asset-rename-scriptEventFunction",
LanguageEditorService.unsafeGetType
|> StateLogicService.getEditorState,
)
->Some,
) :
Result.RelationResult.success()
)
) {
| Success () as result => (
result,
OperateTreeAssetService.updateNode(
nodeId,
ScriptEventFunctionNodeNameAssetService.rename(
~name,
~nodeData,
),
ScriptEventFunctionNodeAssetService.buildNodeByNodeData,
tree,
) =>
result
|> Result.RelationResult.isSuccess
&& NodeAssetService.isIdEqual(nodeId, targetNodeId) ?
{
let (result, newTree) =
switch (
_checkParentNode(parentFolderNode, name, engineState)
|> Result.RelationResult.handleSuccess(() =>
ScriptEventFunctionNodeNameAssetService.isTreeScriptEventFunctionNodesHasTargetName(
name,
tree,
) ?
Result.RelationResult.fail(
LanguageUtils.getMessageLanguageDataByType(
"asset-rename-scriptEventFunction",
LanguageEditorService.unsafeGetType
|> StateLogicService.getEditorState,
)
->Some,
) :
Result.RelationResult.success()
)
) {
| Success () as result => (
result,
OperateTreeAssetService.updateNode(
nodeId,
ScriptEventFunctionNodeNameAssetService.rename(
~name,
~nodeData,
),
)
| Fail(msg) as result => (result, tree)
};

(result, newTree, engineState);
} :
(result, tree, engineState);
ScriptEventFunctionNodeAssetService.buildNodeByNodeData,
tree,
),
)
| Fail(msg) as result => (result, tree)
};

(result, newTree, engineState);
};
(result, newTree, engineState);
} :
(result, tree, engineState);

let _scriptAttributeNodeFunc =
(
(targetNodeId, name),
(targetNodeId, newName),
parentFolderNode,
(result, tree, engineState),
nodeId,
nodeData,
) => {
let (result, newTree, engineState) =
result
|> Result.RelationResult.isSuccess
&& NodeAssetService.isIdEqual(nodeId, targetNodeId) ?
{
let (result, newTree) =
switch (
_checkParentNode(parentFolderNode, name, engineState)
|> Result.RelationResult.handleSuccess(() =>
ScriptAttributeNodeNameAssetService.isTreeScriptAttributeNodesHasTargetName(
name,
tree,
) ?
Result.RelationResult.fail(
LanguageUtils.getMessageLanguageDataByType(
"asset-rename-scriptAttribute",
LanguageEditorService.unsafeGetType
|> StateLogicService.getEditorState,
)
->Some,
) :
Result.RelationResult.success()
) =>
result
|> Result.RelationResult.isSuccess
&& NodeAssetService.isIdEqual(nodeId, targetNodeId) ?
switch (
_checkParentNode(parentFolderNode, newName, engineState)
|> Result.RelationResult.handleSuccess(() =>
ScriptAttributeNodeNameAssetService.isTreeScriptAttributeNodesHasTargetName(
newName,
tree,
) ?
Result.RelationResult.fail(
LanguageUtils.getMessageLanguageDataByType(
"asset-rename-scriptAttribute",
LanguageEditorService.unsafeGetType
|> StateLogicService.getEditorState,
)
) {
| Success () as result => (
result,
OperateTreeAssetService.updateNode(
nodeId,
ScriptAttributeNodeNameAssetService.rename(~name, ~nodeData),
ScriptAttributeNodeAssetService.buildNodeByNodeData,
tree,
),
)
| Fail(msg) as result => (result, tree)
};

(result, newTree, engineState);
} :
(result, tree, engineState);
->Some,
) :
Result.RelationResult.success()
)
) {
| Success () as result =>
let oldName =
ScriptAttributeNodeAssetService.getNodeNameByData(nodeData);
let attribute =
ScriptAttributeNodeAssetService.getAttributeByData(nodeData);

(result, newTree, engineState);
};
(
result,
OperateTreeAssetService.updateNode(
nodeId,
ScriptAttributeNodeNameAssetService.rename(
~name=newName,
~nodeData,
),
ScriptAttributeNodeAssetService.buildNodeByNodeData,
tree,
),
engineState
|> ScriptEngineService.replaceAttributeInAllScriptComponents(
(oldName, newName),
attribute,
),
);
| Fail(msg) as result => (result, tree, engineState)
} :
(result, tree, engineState);

let _wdbNodeFunc =
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,33 @@ let _ =
})
);
});

describe("test rename attribute", () =>
describe("test update script attribute in all script components", () => {
beforeEach(() =>
ScriptAttributeInspectorTool.TestUpdateScriptAttributeInAllScriptComponents.createDefaultSceneAndAddScriptComponent(
sandbox,
)
);

test("test update one script component", () => {
let (script, addedNodeId, fieldName) =
ScriptAttributeInspectorTool.TestUpdateScriptAttributeInAllScriptComponents.prepareForOneScriptComponent(
sandbox,
);
let newAttributeName = "zzz";

AssetTreeInspectorTool.Rename.renameAssetScriptAttributeNode(
~name=newAttributeName,
~nodeId=addedNodeId,
(),
);

ScriptToolEngine.getScriptAttributeEntries(script, newAttributeName)
|> StateLogicService.getEngineStateToGetData
|> Js.Array.length
|> expect == 1;
});
})
);
});

0 comments on commit fee5180

Please sign in to comment.