Bug 1855385 [wpt PR 42195] - [text-spacing-trim] Remove redundant copying of variants...
[gecko.git] / editor / libeditor / PlaceholderTransaction.h
blob8201d797fed0a528cc69f888efb2f8f8eec88b4d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef PlaceholderTransaction_h
7 #define PlaceholderTransaction_h
9 #include "EditAggregateTransaction.h"
10 #include "EditorForwards.h"
11 #include "SelectionState.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/WeakPtr.h"
16 namespace mozilla {
18 /**
19 * An aggregate transaction that knows how to absorb all subsequent
20 * transactions with the same name. This transaction does not "Do" anything.
21 * But it absorbs other transactions via merge, and can undo/redo the
22 * transactions it has absorbed.
25 class PlaceholderTransaction final : public EditAggregateTransaction,
26 public SupportsWeakPtr {
27 protected:
28 PlaceholderTransaction(EditorBase& aEditorBase, nsStaticAtom& aName,
29 Maybe<SelectionState>&& aSelState);
31 public:
32 /**
33 * Creates a placeholder transaction. This never returns nullptr.
35 * @param aEditorBase The editor.
36 * @param aName The name of creating transaction.
37 * @param aSelState The selection state of aEditorBase.
39 static already_AddRefed<PlaceholderTransaction> Create(
40 EditorBase& aEditorBase, nsStaticAtom& aName,
41 Maybe<SelectionState>&& aSelState) {
42 // Make sure to move aSelState into a local variable to null out the
43 // original Maybe<SelectionState> variable.
44 Maybe<SelectionState> selState(std::move(aSelState));
45 RefPtr<PlaceholderTransaction> transaction =
46 new PlaceholderTransaction(aEditorBase, aName, std::move(selState));
47 return transaction.forget();
50 NS_DECL_ISUPPORTS_INHERITED
52 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PlaceholderTransaction,
53 EditAggregateTransaction)
55 NS_DECL_EDITTRANSACTIONBASE
56 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(PlaceholderTransaction)
58 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
59 NS_IMETHOD Merge(nsITransaction* aTransaction, bool* aDidMerge) override;
61 void AppendChild(EditTransactionBase& aTransaction);
63 nsresult EndPlaceHolderBatch();
65 void ForwardEndBatchTo(PlaceholderTransaction& aForwardingTransaction) {
66 mForwardingTransaction = &aForwardingTransaction;
69 void Commit() { mCommitted = true; }
71 nsresult RememberEndingSelection();
73 protected:
74 virtual ~PlaceholderTransaction() = default;
76 // The editor for this transaction.
77 RefPtr<EditorBase> mEditorBase;
79 WeakPtr<PlaceholderTransaction> mForwardingTransaction;
81 // First IME txn in this placeholder - used for IME merging.
82 WeakPtr<CompositionTransaction> mCompositionTransaction;
84 // These next two members store the state of the selection in a safe way.
85 // Selection at the start of the transaction is stored, as is the selection
86 // at the end. This is so that UndoTransaction() and RedoTransaction() can
87 // restore the selection properly.
89 SelectionState mStartSel;
90 SelectionState mEndSel;
92 // Do we auto absorb any and all transaction?
93 bool mAbsorb;
94 // Do we stop auto absorbing any matching placeholder transactions?
95 bool mCommitted;
98 } // namespace mozilla
100 #endif // #ifndef PlaceholderTransaction_h