From f159d161fa6c15aa16cdac6967a2b165d0f9fef0 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sat, 26 Sep 2020 15:29:11 +0330 Subject: Spreadsheet: Let the cells know their own position in the sheet --- Applications/Spreadsheet/Cell.h | 15 +++++++-- Applications/Spreadsheet/JSIntegration.cpp | 31 +++++++++++++++++++ Applications/Spreadsheet/JSIntegration.h | 1 + Applications/Spreadsheet/Position.h | 49 ++++++++++++++++++++++++++++++ Applications/Spreadsheet/Spreadsheet.cpp | 4 +-- Applications/Spreadsheet/Spreadsheet.h | 17 +---------- 6 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 Applications/Spreadsheet/Position.h (limited to 'Applications') diff --git a/Applications/Spreadsheet/Cell.h b/Applications/Spreadsheet/Cell.h index 3942d83c94..04c10a66c0 100644 --- a/Applications/Spreadsheet/Cell.h +++ b/Applications/Spreadsheet/Cell.h @@ -30,6 +30,7 @@ #include "ConditionalFormatting.h" #include "Forward.h" #include "JSIntegration.h" +#include "Position.h" #include #include #include @@ -37,20 +38,22 @@ namespace Spreadsheet { struct Cell : public Weakable { - Cell(String data, WeakPtr sheet) + Cell(String data, Position position, WeakPtr sheet) : dirty(false) , data(move(data)) , kind(LiteralString) , sheet(sheet) + , m_position(move(position)) { } - Cell(String source, JS::Value&& cell_value, WeakPtr sheet) + Cell(String source, JS::Value&& cell_value, Position position, WeakPtr sheet) : dirty(false) , data(move(source)) , evaluated_data(move(cell_value)) , kind(Formula) , sheet(sheet) + , m_position(move(position)) { } @@ -63,6 +66,13 @@ struct Cell : public Weakable { void set_type(const CellType*); void set_type_metadata(CellTypeMetadata&&); + const Position& position() const { return m_position; } + void set_position(Position position, Badge) + { + dirty = true; + m_position = move(position); + } + const Format& evaluated_formats() const { return m_evaluated_formats; } const Vector& conditional_formats() const { return m_conditional_formats; } void set_conditional_formats(Vector&& fmts) @@ -99,6 +109,7 @@ struct Cell : public Weakable { Vector> referencing_cells; const CellType* m_type { nullptr }; CellTypeMetadata m_type_metadata; + Position m_position; Vector m_conditional_formats; Format m_evaluated_formats; diff --git a/Applications/Spreadsheet/JSIntegration.cpp b/Applications/Spreadsheet/JSIntegration.cpp index f393093d8e..26cc864cb7 100644 --- a/Applications/Spreadsheet/JSIntegration.cpp +++ b/Applications/Spreadsheet/JSIntegration.cpp @@ -82,6 +82,7 @@ void SheetGlobalObject::initialize() { GlobalObject::initialize(); define_native_function("parse_cell_name", parse_cell_name, 1); + define_native_function("current_cell_position", current_cell_position, 0); } JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name) @@ -106,6 +107,36 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name) return object; } +JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position) +{ + if (vm.argument_count() != 0) { + vm.throw_exception(global_object, "Expected no arguments to current_cell_position()"); + return {}; + } + + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return JS::js_null(); + + if (StringView("SheetGlobalObject") != this_object->class_name()) { + vm.throw_exception(global_object, JS::ErrorType::NotA, "SheetGlobalObject"); + return {}; + } + + auto sheet_object = static_cast(this_object); + auto* current_cell = sheet_object->m_sheet.current_evaluated_cell(); + if (!current_cell) + return JS::js_null(); + + auto position = current_cell->position(); + + auto object = JS::Object::create_empty(global_object); + object->put("column", JS::js_string(vm, position.column)); + object->put("row", JS::Value((unsigned)position.row)); + + return object; +} + WorkbookObject::WorkbookObject(Workbook& workbook) : JS::Object(*JS::Object::create_empty(workbook.global_object())) , m_workbook(workbook) diff --git a/Applications/Spreadsheet/JSIntegration.h b/Applications/Spreadsheet/JSIntegration.h index 9bddb1e25e..46886f9529 100644 --- a/Applications/Spreadsheet/JSIntegration.h +++ b/Applications/Spreadsheet/JSIntegration.h @@ -45,6 +45,7 @@ public: virtual void initialize() override; JS_DECLARE_NATIVE_FUNCTION(parse_cell_name); + JS_DECLARE_NATIVE_FUNCTION(current_cell_position); private: Sheet& m_sheet; diff --git a/Applications/Spreadsheet/Position.h b/Applications/Spreadsheet/Position.h new file mode 100644 index 0000000000..358ab9c1f6 --- /dev/null +++ b/Applications/Spreadsheet/Position.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +namespace Spreadsheet { + +struct Position { + String column; + size_t row { 0 }; + + bool operator==(const Position& other) const + { + return row == other.row && column == other.column; + } + + bool operator!=(const Position& other) const + { + return !(other == *this); + } +}; + +} diff --git a/Applications/Spreadsheet/Spreadsheet.cpp b/Applications/Spreadsheet/Spreadsheet.cpp index 0519434c1f..254b77ae71 100644 --- a/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Applications/Spreadsheet/Spreadsheet.cpp @@ -245,12 +245,12 @@ RefPtr Sheet::from_json(const JsonObject& object, Workbook& workbook) OwnPtr cell; switch (kind) { case Cell::LiteralString: - cell = make(obj.get("value").to_string(), sheet->make_weak_ptr()); + cell = make(obj.get("value").to_string(), position, sheet->make_weak_ptr()); break; case Cell::Formula: { auto& interpreter = sheet->interpreter(); auto value = interpreter.call(parse_function, json, JS::js_string(interpreter.heap(), obj.get("value").as_string())); - cell = make(obj.get("source").to_string(), move(value), sheet->make_weak_ptr()); + cell = make(obj.get("source").to_string(), move(value), position, sheet->make_weak_ptr()); break; } } diff --git a/Applications/Spreadsheet/Spreadsheet.h b/Applications/Spreadsheet/Spreadsheet.h index 3207398ec8..8d8ad29acc 100644 --- a/Applications/Spreadsheet/Spreadsheet.h +++ b/Applications/Spreadsheet/Spreadsheet.h @@ -41,21 +41,6 @@ namespace Spreadsheet { -struct Position { - String column; - size_t row { 0 }; - - bool operator==(const Position& other) const - { - return row == other.row && column == other.column; - } - - bool operator!=(const Position& other) const - { - return !(other == *this); - } -}; - class Sheet : public Core::Object { C_OBJECT(Sheet); @@ -89,7 +74,7 @@ public: if (auto cell = at(position)) return *cell; - m_cells.set(position, make(String::empty(), make_weak_ptr())); + m_cells.set(position, make(String::empty(), position, make_weak_ptr())); return *at(position); } -- cgit v1.2.3