PlaceholderTransaction.h (3400B)
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/. */ 5 6 #ifndef PlaceholderTransaction_h 7 #define PlaceholderTransaction_h 8 9 #include "EditAggregateTransaction.h" 10 #include "EditorForwards.h" 11 #include "SelectionState.h" 12 13 #include "mozilla/Maybe.h" 14 #include "mozilla/WeakPtr.h" 15 16 namespace mozilla { 17 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. 23 */ 24 25 class PlaceholderTransaction final : public EditAggregateTransaction, 26 public SupportsWeakPtr { 27 protected: 28 PlaceholderTransaction(EditorBase& aEditorBase, nsStaticAtom& aName, 29 Maybe<SelectionState>&& aSelState); 30 31 public: 32 /** 33 * Creates a placeholder transaction. This never returns nullptr. 34 * 35 * @param aEditorBase The editor. 36 * @param aName The name of creating transaction. 37 * @param aSelState The selection state of aEditorBase. 38 */ 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(); 48 } 49 50 NS_DECL_ISUPPORTS_INHERITED 51 52 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PlaceholderTransaction, 53 EditAggregateTransaction) 54 55 NS_DECL_EDITTRANSACTIONBASE 56 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(PlaceholderTransaction) 57 58 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override; 59 NS_IMETHOD Merge(nsITransaction* aTransaction, bool* aDidMerge) override; 60 61 void AppendChild(EditTransactionBase& aTransaction); 62 63 nsresult EndPlaceHolderBatch(); 64 65 void ForwardEndBatchTo(PlaceholderTransaction& aForwardingTransaction) { 66 mForwardingTransaction = &aForwardingTransaction; 67 } 68 69 void Commit() { mCommitted = true; } 70 71 nsresult RememberEndingSelection(); 72 73 protected: 74 virtual ~PlaceholderTransaction() = default; 75 76 // The editor for this transaction. 77 RefPtr<EditorBase> mEditorBase; 78 79 WeakPtr<PlaceholderTransaction> mForwardingTransaction; 80 81 // First IME txn in this placeholder - used for IME merging. 82 WeakPtr<CompositionTransaction> mCompositionTransaction; 83 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. 88 89 SelectionState mStartSel; 90 SelectionState mEndSel; 91 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; 96 }; 97 98 } // namespace mozilla 99 100 #endif // #ifndef PlaceholderTransaction_h