mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
Spreadsheet: Implement state-preserving saves and loads
This commit is contained in:
parent
916e5e8cb3
commit
fdf0660064
5 changed files with 215 additions and 22 deletions
|
@ -111,15 +111,20 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
Sheet::Sheet(const StringView& name)
|
Sheet::Sheet(const StringView& name)
|
||||||
: m_name(name)
|
: Sheet(EmptyConstruct::EmptyConstructTag)
|
||||||
, m_interpreter(JS::Interpreter::create<SheetGlobalObject>(*this))
|
|
||||||
{
|
{
|
||||||
|
m_name = name;
|
||||||
|
|
||||||
for (size_t i = 0; i < 20; ++i)
|
for (size_t i = 0; i < 20; ++i)
|
||||||
add_row();
|
add_row();
|
||||||
|
|
||||||
for (size_t i = 0; i < 16; ++i)
|
for (size_t i = 0; i < 16; ++i)
|
||||||
add_column();
|
add_column();
|
||||||
|
}
|
||||||
|
|
||||||
|
Sheet::Sheet(EmptyConstruct)
|
||||||
|
: m_interpreter(JS::Interpreter::create<SheetGlobalObject>(*this))
|
||||||
|
{
|
||||||
auto file_or_error = Core::File::open("/res/js/Spreadsheet/runtime.js", Core::IODevice::OpenMode::ReadOnly);
|
auto file_or_error = Core::File::open("/res/js/Spreadsheet/runtime.js", Core::IODevice::OpenMode::ReadOnly);
|
||||||
if (!file_or_error.is_error()) {
|
if (!file_or_error.is_error()) {
|
||||||
auto buffer = file_or_error.value()->read_all();
|
auto buffer = file_or_error.value()->read_all();
|
||||||
|
@ -301,6 +306,61 @@ void Cell::reference_from(Cell* other)
|
||||||
referencing_cells.append(other->make_weak_ptr());
|
referencing_cells.append(other->make_weak_ptr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RefPtr<Sheet> Sheet::from_json(const JsonObject& object)
|
||||||
|
{
|
||||||
|
auto sheet = adopt(*new Sheet(EmptyConstruct::EmptyConstructTag));
|
||||||
|
auto rows = object.get("rows").to_u32(20);
|
||||||
|
auto columns = object.get("columns");
|
||||||
|
if (!columns.is_array())
|
||||||
|
return nullptr;
|
||||||
|
auto name = object.get("name").as_string_or("Sheet");
|
||||||
|
|
||||||
|
sheet->set_name(name);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < rows; ++i)
|
||||||
|
sheet->add_row();
|
||||||
|
|
||||||
|
// FIXME: Better error checking.
|
||||||
|
columns.as_array().for_each([&](auto& value) {
|
||||||
|
sheet->m_columns.append(value.as_string());
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
|
||||||
|
auto cells = object.get("cells").as_object();
|
||||||
|
auto json = sheet->interpreter().global_object().get("JSON");
|
||||||
|
auto& parse_function = json.as_object().get("parse").as_function();
|
||||||
|
|
||||||
|
cells.for_each_member([&](auto& name, JsonValue& value) {
|
||||||
|
auto position_option = parse_cell_name(name);
|
||||||
|
if (!position_option.has_value())
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
|
||||||
|
auto position = position_option.value();
|
||||||
|
auto& obj = value.as_object();
|
||||||
|
auto kind = obj.get("kind").as_string_or("LiteralString") == "LiteralString" ? Cell::LiteralString : Cell::Formula;
|
||||||
|
|
||||||
|
OwnPtr<Cell> cell;
|
||||||
|
switch (kind) {
|
||||||
|
case Cell::LiteralString:
|
||||||
|
cell = make<Cell>(obj.get("value").to_string(), sheet->make_weak_ptr());
|
||||||
|
break;
|
||||||
|
case Cell::Formula: {
|
||||||
|
auto& interpreter = sheet->interpreter();
|
||||||
|
JS::MarkedValueList args { interpreter.heap() };
|
||||||
|
args.append(JS::js_string(interpreter, obj.get("value").as_string()));
|
||||||
|
auto value = interpreter.call(parse_function, json, move(args));
|
||||||
|
cell = make<Cell>(obj.get("source").to_string(), move(value), sheet->make_weak_ptr());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sheet->m_cells.set(position, cell.release_nonnull());
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
|
||||||
|
return sheet;
|
||||||
|
}
|
||||||
|
|
||||||
JsonObject Sheet::to_json() const
|
JsonObject Sheet::to_json() const
|
||||||
{
|
{
|
||||||
JsonObject object;
|
JsonObject object;
|
||||||
|
@ -322,7 +382,16 @@ JsonObject Sheet::to_json() const
|
||||||
|
|
||||||
JsonObject data;
|
JsonObject data;
|
||||||
data.set("kind", it.value->kind == Cell::Kind::Formula ? "Formula" : "LiteralString");
|
data.set("kind", it.value->kind == Cell::Kind::Formula ? "Formula" : "LiteralString");
|
||||||
data.set("value", it.value->data);
|
if (it.value->kind == Cell::Formula) {
|
||||||
|
data.set("source", it.value->data);
|
||||||
|
auto json = m_interpreter->global_object().get("JSON");
|
||||||
|
JS::MarkedValueList args(m_interpreter->heap());
|
||||||
|
args.append(it.value->evaluated_data);
|
||||||
|
auto stringified = m_interpreter->call(json.as_object().get("stringify").as_function(), json, move(args));
|
||||||
|
data.set("value", stringified.to_string_without_side_effects());
|
||||||
|
} else {
|
||||||
|
data.set("value", it.value->data);
|
||||||
|
}
|
||||||
|
|
||||||
cells.set(key, move(data));
|
cells.set(key, move(data));
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,15 @@ struct Cell : public Weakable<Cell> {
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Cell(String source, JS::Value&& cell_value, WeakPtr<Sheet> sheet)
|
||||||
|
: dirty(false)
|
||||||
|
, data(move(source))
|
||||||
|
, evaluated_data(move(cell_value))
|
||||||
|
, kind(Formula)
|
||||||
|
, sheet(sheet)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool dirty { false };
|
bool dirty { false };
|
||||||
bool evaluated_externally { false };
|
bool evaluated_externally { false };
|
||||||
String data;
|
String data;
|
||||||
|
@ -131,6 +140,7 @@ public:
|
||||||
static Optional<Position> parse_cell_name(const StringView&);
|
static Optional<Position> parse_cell_name(const StringView&);
|
||||||
|
|
||||||
JsonObject to_json() const;
|
JsonObject to_json() const;
|
||||||
|
static RefPtr<Sheet> from_json(const JsonObject&);
|
||||||
|
|
||||||
const String& name() const { return m_name; }
|
const String& name() const { return m_name; }
|
||||||
void set_name(const StringView& name) { m_name = name; }
|
void set_name(const StringView& name) { m_name = name; }
|
||||||
|
@ -179,7 +189,10 @@ public:
|
||||||
bool has_been_visited(Cell* cell) const { return m_visited_cells_in_update.contains(cell); }
|
bool has_been_visited(Cell* cell) const { return m_visited_cells_in_update.contains(cell); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Sheet(const StringView& name);
|
enum class EmptyConstruct { EmptyConstructTag };
|
||||||
|
|
||||||
|
explicit Sheet(EmptyConstruct);
|
||||||
|
explicit Sheet(const StringView& name);
|
||||||
|
|
||||||
String m_name;
|
String m_name;
|
||||||
Vector<String> m_columns;
|
Vector<String> m_columns;
|
||||||
|
@ -191,7 +204,7 @@ private:
|
||||||
|
|
||||||
size_t m_current_column_name_length { 0 };
|
size_t m_current_column_name_length { 0 };
|
||||||
|
|
||||||
NonnullOwnPtr<JS::Interpreter> m_interpreter;
|
mutable NonnullOwnPtr<JS::Interpreter> m_interpreter;
|
||||||
HashTable<Cell*> m_visited_cells_in_update;
|
HashTable<Cell*> m_visited_cells_in_update;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/JsonObjectSerializer.h>
|
#include <AK/JsonObjectSerializer.h>
|
||||||
|
#include <AK/JsonParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
@ -42,7 +43,8 @@
|
||||||
|
|
||||||
namespace Spreadsheet {
|
namespace Spreadsheet {
|
||||||
|
|
||||||
SpreadsheetWidget::SpreadsheetWidget()
|
SpreadsheetWidget::SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets, bool should_add_sheet_if_empty)
|
||||||
|
: m_sheets(move(sheets))
|
||||||
{
|
{
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
|
set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
|
||||||
|
@ -76,8 +78,23 @@ SpreadsheetWidget::SpreadsheetWidget()
|
||||||
m_tab_widget = container.add<GUI::TabWidget>();
|
m_tab_widget = container.add<GUI::TabWidget>();
|
||||||
m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
|
m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
|
||||||
|
|
||||||
m_sheets.append(Sheet::construct("Sheet 1"));
|
m_cell_value_editor = cell_value_editor;
|
||||||
auto& tab = m_tab_widget->add_tab<SpreadsheetView>(m_sheets.first().name(), m_sheets.first());
|
m_current_cell_label = current_cell_label;
|
||||||
|
|
||||||
|
if (m_sheets.is_empty() && should_add_sheet_if_empty)
|
||||||
|
m_sheets.append(Sheet::construct("Sheet 1"));
|
||||||
|
|
||||||
|
setup_tabs();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpreadsheetWidget::setup_tabs()
|
||||||
|
{
|
||||||
|
RefPtr<GUI::Widget> first_tab_widget;
|
||||||
|
for (auto& sheet : m_sheets) {
|
||||||
|
auto& tab = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
|
||||||
|
if (!first_tab_widget)
|
||||||
|
first_tab_widget = &tab;
|
||||||
|
}
|
||||||
|
|
||||||
auto change = [&](auto& selected_widget) {
|
auto change = [&](auto& selected_widget) {
|
||||||
if (m_selected_view) {
|
if (m_selected_view) {
|
||||||
|
@ -89,26 +106,28 @@ SpreadsheetWidget::SpreadsheetWidget()
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(position.column);
|
builder.append(position.column);
|
||||||
builder.appendf("%zu", position.row);
|
builder.appendf("%zu", position.row);
|
||||||
current_cell_label.set_enabled(true);
|
m_current_cell_label->set_enabled(true);
|
||||||
current_cell_label.set_text(builder.string_view());
|
m_current_cell_label->set_text(builder.string_view());
|
||||||
|
|
||||||
cell_value_editor.on_change = nullptr;
|
m_cell_value_editor->on_change = nullptr;
|
||||||
cell_value_editor.set_text(cell.source());
|
m_cell_value_editor->set_text(cell.source());
|
||||||
cell_value_editor.on_change = [&] {
|
m_cell_value_editor->on_change = [&] {
|
||||||
cell.set_data(cell_value_editor.text());
|
cell.set_data(m_cell_value_editor->text());
|
||||||
m_selected_view->sheet().update();
|
m_selected_view->sheet().update();
|
||||||
};
|
};
|
||||||
cell_value_editor.set_enabled(true);
|
m_cell_value_editor->set_enabled(true);
|
||||||
};
|
};
|
||||||
m_selected_view->on_selection_dropped = [&]() {
|
m_selected_view->on_selection_dropped = [&]() {
|
||||||
cell_value_editor.set_enabled(false);
|
m_cell_value_editor->set_enabled(false);
|
||||||
cell_value_editor.set_text("");
|
m_cell_value_editor->set_text("");
|
||||||
current_cell_label.set_enabled(false);
|
m_current_cell_label->set_enabled(false);
|
||||||
current_cell_label.set_text("");
|
m_current_cell_label->set_text("");
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
change(tab);
|
if (first_tab_widget)
|
||||||
|
change(*first_tab_widget);
|
||||||
|
|
||||||
m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
|
m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
|
||||||
change(selected_widget);
|
change(selected_widget);
|
||||||
};
|
};
|
||||||
|
@ -118,6 +137,66 @@ SpreadsheetWidget::~SpreadsheetWidget()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SpreadsheetWidget::load(const StringView& filename)
|
||||||
|
{
|
||||||
|
auto file_or_error = Core::File::open(filename, Core::IODevice::OpenMode::ReadOnly);
|
||||||
|
if (file_or_error.is_error()) {
|
||||||
|
StringBuilder sb;
|
||||||
|
sb.append("Failed to open ");
|
||||||
|
sb.append(filename);
|
||||||
|
sb.append(" for reading. Error: ");
|
||||||
|
sb.append(file_or_error.error());
|
||||||
|
|
||||||
|
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto json_value_option = JsonParser(file_or_error.value()->read_all()).parse();
|
||||||
|
if (!json_value_option.has_value()) {
|
||||||
|
StringBuilder sb;
|
||||||
|
sb.append("Failed to parse ");
|
||||||
|
sb.append(filename);
|
||||||
|
|
||||||
|
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& json_value = json_value_option.value();
|
||||||
|
if (!json_value.is_array()) {
|
||||||
|
StringBuilder sb;
|
||||||
|
sb.append("Did not find a spreadsheet in ");
|
||||||
|
sb.append(filename);
|
||||||
|
|
||||||
|
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtrVector<Sheet> sheets;
|
||||||
|
|
||||||
|
auto& json_array = json_value.as_array();
|
||||||
|
json_array.for_each([&](auto& sheet_json) {
|
||||||
|
if (!sheet_json.is_object())
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
|
||||||
|
auto sheet = Sheet::from_json(sheet_json.as_object());
|
||||||
|
if (!sheet)
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
|
||||||
|
sheets.append(sheet.release_nonnull());
|
||||||
|
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
|
||||||
|
m_sheets.clear();
|
||||||
|
m_sheets = move(sheets);
|
||||||
|
|
||||||
|
while (auto* widget = m_tab_widget->active_widget()) {
|
||||||
|
m_tab_widget->remove_tab(*widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
setup_tabs();
|
||||||
|
}
|
||||||
|
|
||||||
void SpreadsheetWidget::save(const StringView& filename)
|
void SpreadsheetWidget::save(const StringView& filename)
|
||||||
{
|
{
|
||||||
JsonArray array;
|
JsonArray array;
|
||||||
|
|
|
@ -39,13 +39,18 @@ public:
|
||||||
~SpreadsheetWidget();
|
~SpreadsheetWidget();
|
||||||
|
|
||||||
void save(const StringView& filename);
|
void save(const StringView& filename);
|
||||||
|
void load(const StringView& filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpreadsheetWidget();
|
explicit SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets = {}, bool should_add_sheet_if_empty = true);
|
||||||
|
|
||||||
|
void setup_tabs();
|
||||||
|
|
||||||
NonnullRefPtrVector<Sheet> m_sheets;
|
NonnullRefPtrVector<Sheet> m_sheets;
|
||||||
SpreadsheetView* m_selected_view { nullptr };
|
SpreadsheetView* m_selected_view { nullptr };
|
||||||
RefPtr<GUI::TabWidget> m_tab_widget;
|
RefPtr<GUI::TabWidget> m_tab_widget;
|
||||||
|
RefPtr<GUI::Label> m_current_cell_label;
|
||||||
|
RefPtr<GUI::TextEditor> m_cell_value_editor;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "Spreadsheet.h"
|
#include "Spreadsheet.h"
|
||||||
#include "SpreadsheetWidget.h"
|
#include "SpreadsheetWidget.h"
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/FilePicker.h>
|
#include <LibGUI/FilePicker.h>
|
||||||
#include <LibGUI/Forward.h>
|
#include <LibGUI/Forward.h>
|
||||||
|
@ -36,9 +37,20 @@
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
const char* filename = nullptr;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
|
||||||
|
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (filename) {
|
||||||
|
if (!Core::File::exists(filename) || Core::File::is_directory(filename)) {
|
||||||
|
fprintf(stderr, "File does not exist or is a directory: %s\n", filename);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto app = GUI::Application::construct(argc, argv);
|
auto app = GUI::Application::construct(argc, argv);
|
||||||
|
|
||||||
if (pledge("stdio thread rpath accept cpath wpath shared_buffer unix", nullptr) < 0) {
|
if (pledge("stdio thread rpath accept cpath wpath shared_buffer unix", nullptr) < 0) {
|
||||||
|
@ -56,6 +68,11 @@ int main(int argc, char* argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (unveil(Core::StandardPaths::home_directory().characters(), "rwc") < 0) {
|
||||||
|
perror("unveil");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (unveil("/res", "r") < 0) {
|
if (unveil("/res", "r") < 0) {
|
||||||
perror("unveil");
|
perror("unveil");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -70,7 +87,10 @@ int main(int argc, char* argv[])
|
||||||
window->set_title("Spreadsheet");
|
window->set_title("Spreadsheet");
|
||||||
window->resize(640, 480);
|
window->resize(640, 480);
|
||||||
|
|
||||||
auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>();
|
auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>(NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr);
|
||||||
|
|
||||||
|
if (filename)
|
||||||
|
spreadsheet_widget.load(filename);
|
||||||
|
|
||||||
auto menubar = GUI::MenuBar::construct();
|
auto menubar = GUI::MenuBar::construct();
|
||||||
auto& app_menu = menubar->add_menu("Spreadsheet");
|
auto& app_menu = menubar->add_menu("Spreadsheet");
|
||||||
|
@ -80,6 +100,13 @@ int main(int argc, char* argv[])
|
||||||
}));
|
}));
|
||||||
|
|
||||||
auto& file_menu = menubar->add_menu("File");
|
auto& file_menu = menubar->add_menu("File");
|
||||||
|
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
||||||
|
Optional<String> load_path = GUI::FilePicker::get_open_filepath(window);
|
||||||
|
if (!load_path.has_value())
|
||||||
|
return;
|
||||||
|
|
||||||
|
spreadsheet_widget.load(load_path.value());
|
||||||
|
}));
|
||||||
file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
|
file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
|
||||||
String name = "sheet";
|
String name = "sheet";
|
||||||
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
|
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue