sc/Library_sc.mk                                        |    1 
 sc/source/ui/docshell/docfunc.cxx                       |   69 ---------
 sc/source/ui/inc/operation/ReplaceNoteTextOperation.hxx |   41 +++++
 sc/source/ui/operation/ReplaceNoteTextOperation.cxx     |  117 ++++++++++++++++
 4 files changed, 162 insertions(+), 66 deletions(-)

New commits:
commit 4f0aaa67b0dd3e9081f46fe4030d2df0b8453a22
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Sun Mar 8 03:03:21 2026 +0000
Commit:     Miklos Vajna <[email protected]>
CommitDate: Thu Mar 12 14:16:11 2026 +0100

    sc: Move ScDocFunc::ReplaceNote to ReplaceNoteTextOperation
    
    Replace note used to set or replace the note text for an address.
    
    Pure refactoring, no behavior change.
    
    Change-Id: Ibe4b5c7d54d9a596550adf06dc4456677ef0cd3d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/201473
    Reviewed-by: Miklos Vajna <[email protected]>
    Tested-by: Jenkins CollaboraOffice <[email protected]>

diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 5029063d3b42..babc93479cd1 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -556,6 +556,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
     sc/source/ui/operation/OperationType \
     sc/source/ui/operation/QueryOperation \
     sc/source/ui/operation/RemovePivotTableOperation \
+    sc/source/ui/operation/ReplaceNoteTextOperation \
     sc/source/ui/operation/SetEditTextOperation \
     sc/source/ui/operation/SetNoteTextOperation \
     sc/source/ui/operation/SetFormulaOperation \
diff --git a/sc/source/ui/docshell/docfunc.cxx 
b/sc/source/ui/docshell/docfunc.cxx
index 16b1c01646e3..3c41dfa9a74a 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -128,6 +128,7 @@
 #include <operation/InsertSheetViewOperation.hxx>
 #include <operation/InsertSparklinesOperation.hxx>
 #include <operation/MultipleOpsOperation.hxx>
+#include <operation/ReplaceNoteTextOperation.hxx>
 #include <operation/SetNoteTextOperation.hxx>
 #include <operation/TransliterateTextOperation.hxx>
 #include <operation/UngroupSparklinesOperation.hxx>
@@ -980,72 +981,8 @@ void ScDocFunc::SetNoteText( const ScAddress& rPos, const 
OUString& rText, bool
 
 void ScDocFunc::ReplaceNote( const ScAddress& rPos, const OUString& rNoteText, 
const OUString* pAuthor, const OUString* pDate, bool bApi )
 {
-    ScDocShellModificator aModificator( rDocShell );
-    ScDocument& rDoc = rDocShell.GetDocument();
-
-    if (!CheckSheetViewProtection(sc::OperationType::ReplaceNoteText))
-        return;
-
-    ScEditableTester aTester = ScEditableTester::CreateAndTestBlock(rDoc, 
rPos.Tab(), rPos.Col(), rPos.Row(), rPos.Col(), rPos.Row());
-    if (aTester.IsEditable())
-    {
-        ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
-        SfxUndoManager* pUndoMgr = (pDrawLayer && rDoc.IsUndoEnabled()) ? 
rDocShell.GetUndoManager() : nullptr;
-
-        ScNoteData aOldData;
-        std::unique_ptr<ScPostIt> pOldNote = rDoc.ReleaseNote( rPos );
-        sal_uInt32 nNoteId = 0;
-        if( pOldNote )
-        {
-            nNoteId = pOldNote->GetId();
-            // ensure existing caption object before draw undo tracking starts
-            pOldNote->GetOrCreateCaption( rPos );
-            // rescue note data for undo
-            aOldData = pOldNote->GetNoteData();
-        }
-
-        // collect drawing undo actions for deleting/inserting caption objects
-        if( pUndoMgr )
-            pDrawLayer->BeginCalcUndo(false);
-
-        // delete the note (creates drawing undo action for the caption object)
-        bool hadOldNote(pOldNote);
-        pOldNote.reset();
-
-        // create new note (creates drawing undo action for the new caption 
object)
-        ScNoteData aNewData;
-        ScPostIt* pNewNote = nullptr;
-        if( (pNewNote = ScNoteUtil::CreateNoteFromString( rDoc, rPos, 
rNoteText, false, true, nNoteId )) )
-        {
-            if( pAuthor ) pNewNote->SetAuthor( *pAuthor );
-            if( pDate ) pNewNote->SetDate( *pDate );
-
-            // rescue note data for undo
-            aNewData = pNewNote->GetNoteData();
-        }
-
-        // create the undo action
-        if( pUndoMgr && (aOldData.mxCaption || aNewData.mxCaption) )
-            pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( 
rDocShell, rPos, aOldData, aNewData, pDrawLayer->GetCalcUndo() ) );
-
-        // repaint cell (to make note marker visible)
-        rDocShell.PostPaintCell( rPos );
-
-        rDoc.SetStreamValid(rPos.Tab(), false);
-
-        aModificator.SetDocumentModified();
-
-        // Let our LOK clients know about the new/modified note
-        if (pNewNote)
-        {
-            ScDocShell::LOKCommentNotify(hadOldNote ? 
LOKCommentNotificationType::Modify : LOKCommentNotificationType::Add,
-                                         rDoc, rPos, pNewNote);
-        }
-    }
-    else if (!bApi)
-    {
-        rDocShell.ErrorMessage(aTester.GetMessageId());
-    }
+    sc::ReplaceNoteTextOperation aOperation(rDocShell, rPos, rNoteText, 
pAuthor, pDate, bApi);
+    aOperation.run();
 }
 
 void ScDocFunc::ImportNote( const ScAddress& rPos,
diff --git a/sc/source/ui/inc/operation/ReplaceNoteTextOperation.hxx 
b/sc/source/ui/inc/operation/ReplaceNoteTextOperation.hxx
new file mode 100644
index 000000000000..adaba733d195
--- /dev/null
+++ b/sc/source/ui/inc/operation/ReplaceNoteTextOperation.hxx
@@ -0,0 +1,41 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#include "Operation.hxx"
+#include <address.hxx>
+#include <rtl/ustring.hxx>
+
+#include <optional>
+
+class ScDocShell;
+
+namespace sc
+{
+/** Sets or replaces the content of the note at the input address. */
+class ReplaceNoteTextOperation : public Operation
+{
+private:
+    ScDocShell& mrDocShell;
+    ScAddress maPos;
+    OUString maText;
+    std::optional<OUString> moAuthor;
+    std::optional<OUString> moDate;
+
+    bool runImplementation() override;
+
+public:
+    ReplaceNoteTextOperation(ScDocShell& rDocShell, const ScAddress& rPos,
+                             const OUString& rNoteText, const OUString* 
pAuthor,
+                             const OUString* pDate, bool bApi);
+};
+} // end sc namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/operation/ReplaceNoteTextOperation.cxx 
b/sc/source/ui/operation/ReplaceNoteTextOperation.cxx
new file mode 100644
index 000000000000..325149fe93b9
--- /dev/null
+++ b/sc/source/ui/operation/ReplaceNoteTextOperation.cxx
@@ -0,0 +1,117 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <operation/ReplaceNoteTextOperation.hxx>
+
+#include <svx/svdocapt.hxx>
+
+#include <docsh.hxx>
+#include <drwlayer.hxx>
+#include <editable.hxx>
+#include <postit.hxx>
+#include <undocell.hxx>
+
+namespace sc
+{
+ReplaceNoteTextOperation::ReplaceNoteTextOperation(ScDocShell& rDocShell, 
const ScAddress& rPos,
+                                                   const OUString& rNoteText,
+                                                   const OUString* pAuthor, 
const OUString* pDate,
+                                                   bool bApi)
+    : Operation(OperationType::ReplaceNoteText, false, bApi)
+    , mrDocShell(rDocShell)
+    , maPos(rPos)
+    , maText(rNoteText)
+    , moAuthor(pAuthor ? std::optional<OUString>(*pAuthor) : std::nullopt)
+    , moDate(pDate ? std::optional<OUString>(*pDate) : std::nullopt)
+{
+}
+
+bool ReplaceNoteTextOperation::runImplementation()
+{
+    ScDocShellModificator aModificator(mrDocShell);
+    ScDocument& rDoc = mrDocShell.GetDocument();
+
+    if (!checkSheetViewProtection())
+        return false;
+
+    ScEditableTester aTester = ScEditableTester::CreateAndTestBlock(
+        rDoc, maPos.Tab(), maPos.Col(), maPos.Row(), maPos.Col(), maPos.Row());
+    if (aTester.IsEditable())
+    {
+        ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
+        SfxUndoManager* pUndoMgr
+            = (pDrawLayer && rDoc.IsUndoEnabled()) ? 
mrDocShell.GetUndoManager() : nullptr;
+
+        ScNoteData aOldData;
+        std::unique_ptr<ScPostIt> pOldNote = rDoc.ReleaseNote(maPos);
+        sal_uInt32 nNoteId = 0;
+        if (pOldNote)
+        {
+            nNoteId = pOldNote->GetId();
+            // ensure existing caption object before draw undo tracking starts
+            pOldNote->GetOrCreateCaption(maPos);
+            // rescue note data for undo
+            aOldData = pOldNote->GetNoteData();
+        }
+
+        // collect drawing undo actions for deleting/inserting caption objects
+        if (pUndoMgr)
+            pDrawLayer->BeginCalcUndo(false);
+
+        // delete the note (creates drawing undo action for the caption object)
+        bool hadOldNote(pOldNote);
+        pOldNote.reset();
+
+        // create new note (creates drawing undo action for the new caption 
object)
+        ScNoteData aNewData;
+        ScPostIt* pNewNote = nullptr;
+        if ((pNewNote
+             = ScNoteUtil::CreateNoteFromString(rDoc, maPos, maText, false, 
true, nNoteId)))
+        {
+            if (moAuthor)
+                pNewNote->SetAuthor(*moAuthor);
+            if (moDate)
+                pNewNote->SetDate(*moDate);
+
+            // rescue note data for undo
+            aNewData = pNewNote->GetNoteData();
+        }
+
+        // create the undo action
+        if (pUndoMgr && (aOldData.mxCaption || aNewData.mxCaption))
+            pUndoMgr->AddUndoAction(std::make_unique<ScUndoReplaceNote>(
+                mrDocShell, maPos, aOldData, aNewData, 
pDrawLayer->GetCalcUndo()));
+
+        // repaint cell (to make note marker visible)
+        mrDocShell.PostPaintCell(maPos);
+
+        rDoc.SetStreamValid(maPos.Tab(), false);
+
+        aModificator.SetDocumentModified();
+
+        // Let our LOK clients know about the new/modified note
+        if (pNewNote)
+        {
+            ScDocShell::LOKCommentNotify(hadOldNote ? 
LOKCommentNotificationType::Modify
+                                                    : 
LOKCommentNotificationType::Add,
+                                         rDoc, maPos, pNewNote);
+        }
+
+        return true;
+    }
+    else if (!mbApi)
+    {
+        mrDocShell.ErrorMessage(aTester.GetMessageId());
+    }
+
+    return false;
+}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to