Spreadsheet: Serialise Positions to URLs and add Sheet::from_uri()

This commit is contained in:
AnotherTest 2020-11-02 20:20:15 +03:30 committed by Andreas Kling
parent 6e9c6acc87
commit 821e875bc0
Notes: sideshowbarker 2024-07-19 01:34:25 +09:00
3 changed files with 33 additions and 0 deletions

View file

@ -28,6 +28,7 @@
#include <AK/String.h>
#include <AK/Types.h>
#include <AK/URL.h>
namespace Spreadsheet {
@ -44,6 +45,16 @@ struct Position {
{
return !(other == *this);
}
URL to_url() const
{
URL url;
url.set_protocol("spreadsheet");
url.set_host("cell");
url.set_path(String::formatted("/{}", getpid()));
url.set_fragment(String::formatted("{}{}", column, row));
return url;
}
};
}

View file

@ -32,6 +32,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonParser.h>
#include <AK/TemporaryChange.h>
#include <AK/URL.h>
#include <LibCore/File.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/Function.h>
@ -202,6 +203,24 @@ Optional<Position> Sheet::parse_cell_name(const StringView& name)
return Position { col, row.to_uint().value() };
}
Cell* Sheet::from_url(const URL& url)
{
if (!url.is_valid()) {
dbgln("Invalid url: {}", url.to_string());
return nullptr;
}
if (url.protocol() != "spreadsheet" || url.host() != "cell") {
dbgln("Bad url: {}", url.to_string());
return nullptr;
}
// FIXME: Figure out a way to do this cross-process.
ASSERT(url.path() == String::formatted("/{}", getpid()));
return at(url.fragment());
}
RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
{
auto sheet = adopt(*new Sheet(workbook));

View file

@ -49,6 +49,9 @@ public:
static Optional<Position> parse_cell_name(const StringView&);
Cell* from_url(const URL&);
const Cell* from_url(const URL& url) const { return const_cast<Sheet*>(this)->from_url(url); }
JsonObject to_json() const;
static RefPtr<Sheet> from_json(const JsonObject&, Workbook&);