diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2023-01-20 09:50:11 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2023-01-20 12:41:19 +0000 |
commit | a5a1ea2f7d784c5c6c33f332ba61aceb7af3eca4 (patch) | |
tree | 6aec97d73480ce3f9f47c32ceb8d62a08bfec6f5 /sw/source/uibase/shells/textsh1.cxx | |
parent | 675a3e3d545c5811c35bbd189ff0a241a83443d0 (diff) |
sw: add a new .uno:DeleteSections UNO command
This is similiar to commit 1d6593dd799ff4eb931ffbb5338e4856fb87f77f (sw:
add a new .uno:DeleteFields UNO command, 2023-01-16), but that deleted
refmarks (used for e.g. Zotero citations), while this deletes sections
(used for e.g. Zotero bibliography).
Implement the section "unlinking" (delete the section, but not its data)
by deleting the section format: that will remove the matching section
node as well, but not the content nodes.
Change-Id: Ib00a8f592ddbb77c5e8e08ff94bb0eebfcf7cea8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145870
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
Diffstat (limited to 'sw/source/uibase/shells/textsh1.cxx')
-rw-r--r-- | sw/source/uibase/shells/textsh1.cxx | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/sw/source/uibase/shells/textsh1.cxx b/sw/source/uibase/shells/textsh1.cxx index e887cad14b99..3ef16213576c 100644 --- a/sw/source/uibase/shells/textsh1.cxx +++ b/sw/source/uibase/shells/textsh1.cxx @@ -451,6 +451,49 @@ void UpdateSections(SfxRequest& rReq, SwWrtShell& rWrtSh) rWrtSh.GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::INSSECTION, nullptr); } +void DeleteSections(SfxRequest& rReq, SwWrtShell& rWrtSh) +{ + OUString aSectionNamePrefix; + const SfxStringItem* pSectionNamePrefix = rReq.GetArg<SfxStringItem>(FN_PARAM_1); + if (pSectionNamePrefix) + { + aSectionNamePrefix = pSectionNamePrefix->GetValue(); + } + + rWrtSh.GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELSECTION, nullptr); + rWrtSh.StartAction(); + comphelper::ScopeGuard g( + [&rWrtSh] + { + rWrtSh.EndAction(); + rWrtSh.GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::DELSECTION, nullptr); + }); + + SwDoc* pDoc = rWrtSh.GetDoc(); + SwSectionFormats& rFormats = pDoc->GetSections(); + std::vector<SwSectionFormat*> aRemovals; + for (size_t i = 0; i < rFormats.size(); ++i) + { + SwSectionFormat* pFormat = rFormats[i]; + + if (!aSectionNamePrefix.isEmpty()) + { + if (!pFormat->GetName().startsWith(aSectionNamePrefix)) + { + continue; + } + } + + aRemovals.push_back(pFormat); + } + + for (const auto& pFormat : aRemovals) + { + // Just delete the format, not the content of the section. + pDoc->DelSectionFormat(pFormat); + } +} + void UpdateBookmarks(SfxRequest& rReq, SwWrtShell& rWrtSh) { if (rWrtSh.getIDocumentSettingAccess().get(DocumentSettingId::PROTECT_BOOKMARKS)) @@ -1128,6 +1171,13 @@ void SwTextShell::Execute(SfxRequest &rReq) UpdateSections(rReq, rWrtSh); break; } + case FN_DELETE_SECTIONS: + { + // This deletes all sections in the document matching a specified prefix. Note that the + // section is deleted, but not its contents. + DeleteSections(rReq, rWrtSh); + break; + } case FN_SET_REMINDER: { // collect and sort navigator reminder names |