diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-09-26 15:29:11 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-28 17:41:48 +0200 |
commit | f159d161fa6c15aa16cdac6967a2b165d0f9fef0 (patch) | |
tree | ae967b4d7bcf1c742c56083c7f03b82bde53f117 /Applications | |
parent | 13ce24de135805c250ca64e32b8f911004073a69 (diff) | |
download | serenity-f159d161fa6c15aa16cdac6967a2b165d0f9fef0.zip |
Spreadsheet: Let the cells know their own position in the sheet
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/Spreadsheet/Cell.h | 15 | ||||
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.cpp | 31 | ||||
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.h | 1 | ||||
-rw-r--r-- | Applications/Spreadsheet/Position.h | 49 | ||||
-rw-r--r-- | Applications/Spreadsheet/Spreadsheet.cpp | 4 | ||||
-rw-r--r-- | Applications/Spreadsheet/Spreadsheet.h | 17 |
6 files changed, 97 insertions, 20 deletions
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 <AK/String.h> #include <AK/Types.h> #include <AK/WeakPtr.h> @@ -37,20 +38,22 @@ namespace Spreadsheet { struct Cell : public Weakable<Cell> { - Cell(String data, WeakPtr<Sheet> sheet) + Cell(String data, Position position, WeakPtr<Sheet> sheet) : dirty(false) , data(move(data)) , kind(LiteralString) , sheet(sheet) + , m_position(move(position)) { } - Cell(String source, JS::Value&& cell_value, WeakPtr<Sheet> sheet) + Cell(String source, JS::Value&& cell_value, Position position, WeakPtr<Sheet> 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<Cell> { void set_type(const CellType*); void set_type_metadata(CellTypeMetadata&&); + const Position& position() const { return m_position; } + void set_position(Position position, Badge<Sheet>) + { + dirty = true; + m_position = move(position); + } + const Format& evaluated_formats() const { return m_evaluated_formats; } const Vector<ConditionalFormat>& conditional_formats() const { return m_conditional_formats; } void set_conditional_formats(Vector<ConditionalFormat>&& fmts) @@ -99,6 +109,7 @@ struct Cell : public Weakable<Cell> { Vector<WeakPtr<Cell>> referencing_cells; const CellType* m_type { nullptr }; CellTypeMetadata m_type_metadata; + Position m_position; Vector<ConditionalFormat> 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<JS::TypeError>(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<JS::TypeError>(global_object, JS::ErrorType::NotA, "SheetGlobalObject"); + return {}; + } + + auto sheet_object = static_cast<SheetGlobalObject*>(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 <AK/String.h> +#include <AK/Types.h> + +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> Sheet::from_json(const JsonObject& object, Workbook& workbook) OwnPtr<Cell> cell; switch (kind) { case Cell::LiteralString: - cell = make<Cell>(obj.get("value").to_string(), sheet->make_weak_ptr()); + cell = make<Cell>(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<Cell>(obj.get("source").to_string(), move(value), sheet->make_weak_ptr()); + cell = make<Cell>(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<Cell>(String::empty(), make_weak_ptr())); + m_cells.set(position, make<Cell>(String::empty(), position, make_weak_ptr())); return *at(position); } |