diff options
-rw-r--r-- | Applications/Spreadsheet/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Applications/Spreadsheet/Cell.cpp | 39 | ||||
-rw-r--r-- | Applications/Spreadsheet/Cell.h | 13 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Identity.cpp | 52 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Identity.h | 42 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Numeric.cpp | 62 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Numeric.h | 42 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/String.cpp | 57 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/String.h | 42 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Type.cpp | 52 | ||||
-rw-r--r-- | Applications/Spreadsheet/CellType/Type.h | 53 | ||||
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.cpp | 2 | ||||
-rw-r--r-- | Applications/Spreadsheet/SpreadsheetModel.cpp | 19 |
13 files changed, 468 insertions, 11 deletions
diff --git a/Applications/Spreadsheet/CMakeLists.txt b/Applications/Spreadsheet/CMakeLists.txt index 5ec5efee9a..e3c4a0766d 100644 --- a/Applications/Spreadsheet/CMakeLists.txt +++ b/Applications/Spreadsheet/CMakeLists.txt @@ -1,6 +1,10 @@ set(SOURCES Cell.cpp CellSyntaxHighlighter.cpp + CellType/Identity.cpp + CellType/Numeric.cpp + CellType/String.cpp + CellType/Type.cpp HelpWindow.cpp JSIntegration.cpp Spreadsheet.cpp diff --git a/Applications/Spreadsheet/Cell.cpp b/Applications/Spreadsheet/Cell.cpp index d44d02debf..d7beec0bb3 100644 --- a/Applications/Spreadsheet/Cell.cpp +++ b/Applications/Spreadsheet/Cell.cpp @@ -60,6 +60,45 @@ void Cell::set_data(JS::Value new_data) evaluated_data = move(new_data); } +void Cell::set_type(const StringView& name) +{ + auto* cell_type = CellType::get_by_name(name); + if (cell_type) { + m_type = cell_type; + return; + } + + ASSERT_NOT_REACHED(); +} + +void Cell::set_type_metadata(CellTypeMetadata&& metadata) +{ + m_type_metadata = move(metadata); +} + +const CellType& Cell::type() const +{ + if (m_type) + return *m_type; + + if (kind == LiteralString) { + if (data.to_int().has_value()) + return *CellType::get_by_name("Numeric"); + } + + return *CellType::get_by_name("Identity"); +} + +String Cell::typed_display() const +{ + return type().display(const_cast<Cell&>(*this), m_type_metadata); +} + +JS::Value Cell::typed_js_data() const +{ + return type().js_value(const_cast<Cell&>(*this), m_type_metadata); +} + void Cell::update_data() { TemporaryChange cell_change { sheet->current_evaluated_cell(), this }; diff --git a/Applications/Spreadsheet/Cell.h b/Applications/Spreadsheet/Cell.h index 6fed58f4b9..f7dd3c71dd 100644 --- a/Applications/Spreadsheet/Cell.h +++ b/Applications/Spreadsheet/Cell.h @@ -26,6 +26,7 @@ #pragma once +#include "CellType/Type.h" #include "Forward.h" #include "JSIntegration.h" #include <AK/String.h> @@ -57,6 +58,16 @@ struct Cell : public Weakable<Cell> { void set_data(String new_data); void set_data(JS::Value new_data); + void set_type(const StringView& name); + void set_type_metadata(CellTypeMetadata&&); + + String typed_display() const; + JS::Value typed_js_data() const; + + const CellType& type() const; + const CellTypeMetadata& type_metadata() const { return m_type_metadata; } + CellTypeMetadata& type_metadata() { return m_type_metadata; } + String source() const; JS::Value js_data(); @@ -76,6 +87,8 @@ struct Cell : public Weakable<Cell> { Kind kind { LiteralString }; WeakPtr<Sheet> sheet; Vector<WeakPtr<Cell>> referencing_cells; + const CellType* m_type { nullptr }; + CellTypeMetadata m_type_metadata; private: void update_data(); diff --git a/Applications/Spreadsheet/CellType/Identity.cpp b/Applications/Spreadsheet/CellType/Identity.cpp new file mode 100644 index 0000000000..eed7cb190c --- /dev/null +++ b/Applications/Spreadsheet/CellType/Identity.cpp @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#include "Identity.h" +#include "../Cell.h" +#include "../Spreadsheet.h" + +namespace Spreadsheet { + +IdentityCell::IdentityCell() + : CellType("Identity") +{ +} + +IdentityCell::~IdentityCell() +{ +} + +String IdentityCell::display(Cell& cell, const CellTypeMetadata&) const +{ + return cell.js_data().to_string_without_side_effects(); +} + +JS::Value IdentityCell::js_value(Cell& cell, const CellTypeMetadata&) const +{ + return cell.js_data(); +} + +} diff --git a/Applications/Spreadsheet/CellType/Identity.h b/Applications/Spreadsheet/CellType/Identity.h new file mode 100644 index 0000000000..09089153ec --- /dev/null +++ b/Applications/Spreadsheet/CellType/Identity.h @@ -0,0 +1,42 @@ +/* + * 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 "Type.h" + +namespace Spreadsheet { + +class IdentityCell : public CellType { + +public: + IdentityCell(); + virtual ~IdentityCell() override; + virtual String display(Cell&, const CellTypeMetadata&) const override; + virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override; +}; + +} diff --git a/Applications/Spreadsheet/CellType/Numeric.cpp b/Applications/Spreadsheet/CellType/Numeric.cpp new file mode 100644 index 0000000000..4218e0d1b2 --- /dev/null +++ b/Applications/Spreadsheet/CellType/Numeric.cpp @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#include "Numeric.h" +#include "../Cell.h" +#include "../Spreadsheet.h" + +namespace Spreadsheet { + +NumericCell::NumericCell() + : CellType("Numeric") +{ +} + +NumericCell::~NumericCell() +{ +} + +String NumericCell::display(Cell& cell, const CellTypeMetadata& metadata) const +{ + auto value = js_value(cell, metadata); + String string; + if (metadata.format.is_null()) + string = value.to_string_without_side_effects(); + else + string = String::format(metadata.format.characters(), value.to_double(cell.sheet->interpreter())); // FIXME: Somehow make this format less dependent on 'double'. + + if (metadata.length >= 0) + return string.substring(0, metadata.length); + + return string; +} + +JS::Value NumericCell::js_value(Cell& cell, const CellTypeMetadata&) const +{ + return cell.js_data().to_number(cell.sheet->interpreter()); +} + +} diff --git a/Applications/Spreadsheet/CellType/Numeric.h b/Applications/Spreadsheet/CellType/Numeric.h new file mode 100644 index 0000000000..e0cd4a295a --- /dev/null +++ b/Applications/Spreadsheet/CellType/Numeric.h @@ -0,0 +1,42 @@ +/* + * 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 "Type.h" + +namespace Spreadsheet { + +class NumericCell : public CellType { + +public: + NumericCell(); + virtual ~NumericCell() override; + virtual String display(Cell&, const CellTypeMetadata&) const override; + virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override; +}; + +} diff --git a/Applications/Spreadsheet/CellType/String.cpp b/Applications/Spreadsheet/CellType/String.cpp new file mode 100644 index 0000000000..e8b0ed61d2 --- /dev/null +++ b/Applications/Spreadsheet/CellType/String.cpp @@ -0,0 +1,57 @@ +/* + * 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. + */ + +#include "String.h" +#include "../Cell.h" +#include "../Spreadsheet.h" + +namespace Spreadsheet { + +StringCell::StringCell() + : CellType("String") +{ +} + +StringCell::~StringCell() +{ +} + +String StringCell::display(Cell& cell, const CellTypeMetadata& metadata) const +{ + auto string = cell.js_data().to_string_without_side_effects(); + if (metadata.length >= 0) + return string.substring(0, metadata.length); + + return string; +} + +JS::Value StringCell::js_value(Cell& cell, const CellTypeMetadata& metadata) const +{ + auto string = display(cell, metadata); + return JS::js_string(cell.sheet->interpreter(), string); +} + +} diff --git a/Applications/Spreadsheet/CellType/String.h b/Applications/Spreadsheet/CellType/String.h new file mode 100644 index 0000000000..e4ba28eb24 --- /dev/null +++ b/Applications/Spreadsheet/CellType/String.h @@ -0,0 +1,42 @@ +/* + * 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 "Type.h" + +namespace Spreadsheet { + +class StringCell : public CellType { + +public: + StringCell(); + virtual ~StringCell() override; + virtual String display(Cell&, const CellTypeMetadata&) const override; + virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const override; +}; + +} diff --git a/Applications/Spreadsheet/CellType/Type.cpp b/Applications/Spreadsheet/CellType/Type.cpp new file mode 100644 index 0000000000..80933c1a3d --- /dev/null +++ b/Applications/Spreadsheet/CellType/Type.cpp @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#include "Type.h" +#include "Identity.h" +#include "Numeric.h" +#include "String.h" +#include <AK/HashMap.h> +#include <AK/OwnPtr.h> + +static HashMap<String, Spreadsheet::CellType*> s_cell_types; +static Spreadsheet::StringCell s_string_cell; +static Spreadsheet::NumericCell s_numeric_cell; +static Spreadsheet::IdentityCell s_identity_cell; + +namespace Spreadsheet { + +const CellType* CellType::get_by_name(const StringView& name) +{ + return s_cell_types.get(name).value_or(nullptr); +} + +CellType::CellType(const StringView& name) +{ + ASSERT(!s_cell_types.contains(name)); + s_cell_types.set(name, this); +} + +} diff --git a/Applications/Spreadsheet/CellType/Type.h b/Applications/Spreadsheet/CellType/Type.h new file mode 100644 index 0000000000..7ed9dbe763 --- /dev/null +++ b/Applications/Spreadsheet/CellType/Type.h @@ -0,0 +1,53 @@ +/* + * 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 "../Forward.h" +#include <AK/Forward.h> +#include <AK/String.h> +#include <LibJS/Forward.h> + +namespace Spreadsheet { + +struct CellTypeMetadata { + int length { -1 }; + String format; +}; + +class CellType { +public: + static const CellType* get_by_name(const StringView&); + + virtual String display(Cell&, const CellTypeMetadata&) const = 0; + virtual JS::Value js_value(Cell&, const CellTypeMetadata&) const = 0; + virtual ~CellType() { } + +protected: + CellType(const StringView& name); +}; + +} diff --git a/Applications/Spreadsheet/JSIntegration.cpp b/Applications/Spreadsheet/JSIntegration.cpp index 830b664f6f..2431b5e409 100644 --- a/Applications/Spreadsheet/JSIntegration.cpp +++ b/Applications/Spreadsheet/JSIntegration.cpp @@ -49,7 +49,7 @@ JS::Value SheetGlobalObject::get(const JS::PropertyName& name, JS::Value receive if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) { auto& cell = m_sheet.ensure(pos.value()); cell.reference_from(m_sheet.current_evaluated_cell()); - return cell.js_data(); + return cell.typed_js_data(); } } diff --git a/Applications/Spreadsheet/SpreadsheetModel.cpp b/Applications/Spreadsheet/SpreadsheetModel.cpp index 2b92619784..c4c5c286a6 100644 --- a/Applications/Spreadsheet/SpreadsheetModel.cpp +++ b/Applications/Spreadsheet/SpreadsheetModel.cpp @@ -50,34 +50,33 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) return {}; if (role == GUI::ModelRole::Display) { - const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() }); - if (!value) + const auto* cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() }); + if (!cell) return String::empty(); - if (value->kind == Spreadsheet::Cell::Formula) { - if (auto object = as_error(value->evaluated_data)) { + if (cell->kind == Spreadsheet::Cell::Formula) { + if (auto object = as_error(cell->evaluated_data)) { StringBuilder builder; auto error = object->get("message").to_string_without_side_effects(); builder.append("Error: "); builder.append(error); return builder.to_string(); } - return value->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects(); } - return value->data; + return cell->typed_display(); } if (role == GUI::ModelRole::TextAlignment) return {}; if (role == GUI::ModelRole::ForegroundColor) { - const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() }); - if (!value) + const auto* cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() }); + if (!cell) return {}; - if (value->kind == Spreadsheet::Cell::Formula) { - if (as_error(value->evaluated_data)) + if (cell->kind == Spreadsheet::Cell::Formula) { + if (as_error(cell->evaluated_data)) return Color(Color::Red); } |