diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-11-28 09:06:23 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-11-28 11:18:51 +0100 |
commit | 7765b442e13048f857fd7ee49ced1731caee297e (patch) | |
tree | 488cf04399f01c2504b144b9582b55fbe8f6cc64 /sw/qa/uibase/shells | |
parent | b1b77b4005ceaeb2b9bc11d8a405bb5a467e1d86 (diff) |
sw: add a new .uno:TextFormFields UNO command
Currently .uno:TextFormField can be used to insert a new fieldmark, but
then there is no way to update it. Also, there is no way to update
several fieldmarks at the same time.
Given that the "ADDON" field type in a field mark can be used by
extensions like Zotero, it's useful in case these fieldmarks can be not
only inserted, but they can be also updated. This works by (in the LOK
case) calling getCommandValues() with .uno:TextFormFields, and then once
the client generates the new expanded values, there is no way to insert
these updated expanded values into the document currently.
Fix this by adding a new .uno:TextFormFields UNO command that can update
all fieldmarks matching a certain prefix (looking at their field
command). This allows e.g. updating all Zotero citations or the
bibliography, but the API is generic to support any kind of ADDON
fields. Similar to insertion, the content can be multi-paragraph,
formatted HTML.
This required adjusting SfxUnoAnyItem::CreateDefault(), this way an UNO
command parameter can be an array of beans::PropertyValues, which is how
the client can provide details of multiple fieldmarks.
Change-Id: I44a1b1495ead79b92ccd0c9f6412a34cbec5d68b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143361
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
Diffstat (limited to 'sw/qa/uibase/shells')
-rw-r--r-- | sw/qa/uibase/shells/shells.cxx | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/sw/qa/uibase/shells/shells.cxx b/sw/qa/uibase/shells/shells.cxx index 4361920b3e87..01be5fda10b3 100644 --- a/sw/qa/uibase/shells/shells.cxx +++ b/sw/qa/uibase/shells/shells.cxx @@ -26,6 +26,7 @@ #include <comphelper/propertyvalue.hxx> #include <unotools/ucbstreamhelper.hxx> #include <xmloff/odffields.hxx> +#include <comphelper/string.hxx> #include <IDocumentContentOperations.hxx> #include <cmdid.h> @@ -35,6 +36,7 @@ #include <IDocumentDrawModelAccess.hxx> #include <drawdoc.hxx> #include <docsh.hxx> +#include <ndtxt.hxx> /// Covers sw/source/uibase/shells/ fixes. class SwUibaseShellsTest : public SwModelTestBase @@ -314,6 +316,67 @@ CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormField) CPPUNIT_ASSERT_EQUAL(OUString("aaa\nbbb"), aActualResult); } +CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateFieldmarks) +{ + // Given a document with 2 fieldmarks: + createSwDoc(); + { + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))), + comphelper::makePropertyValue("FieldCommand", + uno::Any(OUString("ADDIN ZOTERO_ITEM old command 1"))), + comphelper::makePropertyValue("FieldResult", uno::Any(OUString("old result 1"))), + }; + dispatchCommand(mxComponent, ".uno:TextFormField", aArgs); + } + { + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))), + comphelper::makePropertyValue("FieldCommand", + uno::Any(OUString("ADDIN ZOTERO_ITEM old command 2"))), + comphelper::makePropertyValue("FieldResult", uno::Any(OUString("old result 2"))), + }; + dispatchCommand(mxComponent, ".uno:TextFormField", aArgs); + } + + // When updating those fieldmarks: + uno::Sequence<css::beans::PropertyValue> aField1{ + comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))), + comphelper::makePropertyValue("FieldCommand", + uno::Any(OUString("ADDIN ZOTERO_ITEM new command 1"))), + comphelper::makePropertyValue("FieldResult", uno::Any(OUString("new result 1"))), + }; + uno::Sequence<css::beans::PropertyValue> aField2{ + comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))), + comphelper::makePropertyValue("FieldCommand", + uno::Any(OUString("ADDIN ZOTERO_ITEM new command 2"))), + comphelper::makePropertyValue("FieldResult", uno::Any(OUString("new result 2"))), + }; + uno::Sequence<uno::Sequence<css::beans::PropertyValue>> aFields = { aField1, aField2 }; + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))), + comphelper::makePropertyValue("FieldCommandPrefix", + uno::Any(OUString("ADDIN ZOTERO_ITEM"))), + comphelper::makePropertyValue("Fields", uno::Any(aFields)), + }; + dispatchCommand(mxComponent, ".uno:TextFormFields", aArgs); + + // Then make sure that the document text contains the new field results: + SwDoc* pDoc = getSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->SttEndDoc(/*bStt=*/true); + SwCursor* pCursor = pWrtShell->GetCursor(); + OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText(); + static sal_Unicode const aForbidden[] + = { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 }; + aActual = comphelper::string::removeAny(aActual, aForbidden); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: new result 1new result 2 + // - Actual : old result 1old result 2 + // i.e. the fieldmarks were not updated. + CPPUNIT_ASSERT_EQUAL(OUString("new result 1new result 2"), aActual); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |