载入中...
搜索中...
未找到
WorkspacePresentation.cpp
浏览该文件的文档.
2
3namespace eve::editor {
4namespace {
5Result<void> failure(EditorStatus status, std::string message) {
6 return editing::failed<void>(status, RuleId("editor.presentation"), std::move(message));
7}
8} // namespace
9
11 if (draft_ && draft_->path != path)
12 return failure(EditorStatus::Conflict, "Finish or cancel the active field before editing another field");
13 const auto state = source.state();
14 if (draft_ && (draft_->start.revision != state.revision || draft_->start.context != state.context)) {
15 cancel();
16 return failure(EditorStatus::Conflict, "Document or selection changed during editing");
17 }
18 const auto descriptor = source.schema().find(path);
19 if (!descriptor) return failure(EditorStatus::NotFound, "Property is unavailable: " + path.value());
20 if (hasPropertyFlag(descriptor->flags, PropertyFlag::ReadOnly))
21 return failure(EditorStatus::Rejected, "Property is read-only: " + path.value());
22 auto valid = validatePropertyValue(*descriptor, value);
23 if (!valid.ok()) {
24 cancel();
25 return valid;
26 }
27 if (descriptor->type == PropertyType::Float)
28 if (const auto* integer = value.getIf<std::int64_t>()) value = static_cast<double>(*integer);
29 const auto current = source.read(path);
30 if (current.state != PropertyReadState::Value)
31 return failure(EditorStatus::Rejected, "Property has no single editable value: " + path.value());
32 if (!draft_)
33 draft_ = Draft{std::move(path), std::move(value), current.value, state};
34 else
35 draft_->value = std::move(value);
36 return editing::applied<void>();
37}
38
40 if (!draft_) return editing::noOp();
41 Draft draft = std::move(*draft_);
42 cancel();
43 const auto state = source.state();
44 if (draft.start.revision != state.revision || draft.start.context != state.context)
45 return failure(EditorStatus::Conflict, "Document or selection changed during editing");
46 if (draft.value == draft.original) return editing::noOp();
47 const auto descriptor = source.schema().find(draft.path);
48 if (!descriptor || hasPropertyFlag(descriptor->flags, PropertyFlag::ReadOnly))
49 return failure(EditorStatus::Rejected, "Property became unavailable during editing");
50 auto valid = validatePropertyValue(*descriptor, draft.value);
51 if (!valid.ok()) return valid;
52 return source.write(draft.path, draft.value);
53}
54
55void WorkspacePresentation::cancel() noexcept { draft_.reset(); }
56
58 auto staged = stage(source, std::move(path), std::move(value));
59 if (!staged.ok()) return staged;
60 return commit(source);
61}
62
64 cancel();
65 const auto state = source.state();
66 if ((id == "undo" && !state.canUndo) || (id == "redo" && !state.canRedo))
67 return failure(EditorStatus::Rejected, "History action is unavailable");
68 return source.action(id);
69}
70
72 const auto state = source.state();
74 for (const auto& descriptor : source.schema().properties) {
75 const auto current = source.read(descriptor.path);
76 std::string kind;
77 switch (descriptor.type) {
78 case PropertyType::Bool: kind = "bool"; break;
79 case PropertyType::Float:
80 case PropertyType::Int: kind = "number"; break;
81 case PropertyType::Enum: kind = "enum"; break;
82 default: kind = "text"; break;
83 }
85 for (const auto& choice : descriptor.enumItems) choices.emplace_back(choice);
86 const bool editable = !hasPropertyFlag(descriptor.flags, PropertyFlag::ReadOnly) &&
87 current.state == PropertyReadState::Value &&
88 (kind != "text" || descriptor.type == PropertyType::String ||
89 descriptor.type == PropertyType::AssetRef || descriptor.type == PropertyType::ObjectRef);
91 if (draft_ && draft_->path == descriptor.path) value = draft_->value;
92 auto projected = toPresentationValue(value);
93 std::string display;
94 if (current.state == PropertyReadState::Mixed)
95 display = "Mixed values";
96 else if (current.state != PropertyReadState::Value)
97 display = "Unavailable";
98 else if (const auto* text = projected.getIf<std::string>())
99 display = *text;
100 else {
101 auto encoded = projected.toJson();
102 display = encoded.ok() ? encoded.value() : "Unavailable";
103 }
104 rows.emplace_back(Value::Object{{"path", descriptor.path.value()},
105 {"label", descriptor.displayNameKey},
106 {"kind", kind},
107 {"value", std::move(projected)},
108 {"display", std::move(display)},
109 {"editable", editable},
110 {"minimum", descriptor.numeric.minimum.value_or(-1000000.0)},
111 {"maximum", descriptor.numeric.maximum.value_or(1000000.0)},
112 {"choices", std::move(choices)}});
113 }
114 return Value::Object{{"revision", static_cast<std::int64_t>(state.revision)},
115 {"context", state.context},
116 {"canUndo", state.canUndo},
117 {"canRedo", state.canRedo},
118 {"playing", state.playing},
119 {"rows", std::move(rows)}};
120}
121} // namespace eve::editor
std::string descriptor
int rows
std::string message
wgpu::PopErrorScopeStatus status
std::set< std::string > choices
bool valid
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
RoadPresetDraft draft
std::string text
std::string_view source
std::string kind
double current
Move-only operation result carrying either a value or Status.
Definition Result.h:176
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Dotted property path (layout.size) compared and persisted as text.
Definition EditingIds.h:78
Deterministic owning value tree shared by authoring hosts.
Synchronous adapter for an open domain editor, without UI or domain includes. @ownership Caller owns ...
Value snapshot(const IWorkspacePresentationSource &source) const
Immutable current schema/value projection, including any local draft.
void cancel() noexcept
Cancel the draft without changing the domain or history.
Result< void > action(IWorkspacePresentationSource &source, std::string_view id)
Route an action through the domain backend; pending drafts are cancelled first.
Result< void > edit(IWorkspacePresentationSource &source, PropertyPath path, EditorValue value)
Validate and commit one discrete keyboard/checkbox edit.
Result< void > stage(IWorkspacePresentationSource &source, PropertyPath path, EditorValue value)
Stage an owning draft; repeated updates create no document/history mutation.
Result< void > commit(IWorkspacePresentationSource &source)
Commit the draft once; rejects revision or selection changes and consumes the draft.
Result< void > noOp(std::vector< Diagnostic > diagnostics={})
Construct a successful void result for an operation that made no change.
eve::editing::RuleId RuleId
Definition EditorIds.h:32
eve::StatusCode EditorStatus
Definition EditorResult.h:8