Changeset 240328 in webkit


Ignore:
Timestamp:
Jan 22, 2019 10:10:45 PM (5 years ago)
Author:
Wenson Hsieh
Message:

Introduce CustomUndoStep.h and CustomUndoStep.cpp
https://bugs.webkit.org/show_bug.cgi?id=193704
<rdar://problem/44807048>

Reviewed by Ryosuke Niwa.

This patch is more work in progress towards supporting UndoManager.addItem(). Here, we introduce a helper
class, CustomUndoStep, that holds a weak reference to a script-defined UndoItem. See below for more details.

No change in behavior.

  • Sources.txt:
  • WebCore.xcodeproj/project.pbxproj:
  • editing/CustomUndoStep.cpp:

(WebCore::CustomUndoStep::CustomUndoStep):

Subclass UndoStep.

(WebCore::CustomUndoStep::unapply):
(WebCore::CustomUndoStep::reapply):

If possible, invoke the UndoItem's undo and redo handlers.

(WebCore::CustomUndoStep::isValid const):

  • editing/CustomUndoStep.h:
  • editing/EditingStyle.cpp:
  • editing/InsertEditableImageCommand.cpp:

(WebCore::InsertEditableImageCommand::doApply):

Unified build fixes.

  • page/UndoItem.h:
Location:
trunk/Source/WebCore
Files:
6 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r240326 r240328  
     12019-01-22  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        Introduce CustomUndoStep.h and CustomUndoStep.cpp
     4        https://bugs.webkit.org/show_bug.cgi?id=193704
     5        <rdar://problem/44807048>
     6
     7        Reviewed by Ryosuke Niwa.
     8
     9        This patch is more work in progress towards supporting `UndoManager.addItem()`. Here, we introduce a helper
     10        class, CustomUndoStep, that holds a weak reference to a script-defined UndoItem. See below for more details.
     11
     12        No change in behavior.
     13
     14        * Sources.txt:
     15        * WebCore.xcodeproj/project.pbxproj:
     16        * editing/CustomUndoStep.cpp:
     17        (WebCore::CustomUndoStep::CustomUndoStep):
     18
     19        Subclass UndoStep.
     20
     21        (WebCore::CustomUndoStep::unapply):
     22        (WebCore::CustomUndoStep::reapply):
     23
     24        If possible, invoke the UndoItem's undo and redo handlers.
     25
     26        (WebCore::CustomUndoStep::isValid const):
     27        * editing/CustomUndoStep.h:
     28        * editing/EditingStyle.cpp:
     29        * editing/InsertEditableImageCommand.cpp:
     30        (WebCore::InsertEditableImageCommand::doApply):
     31
     32        Unified build fixes.
     33
     34        * page/UndoItem.h:
     35
    1362019-01-22  Simon Fraser  <simon.fraser@apple.com>
    237
  • trunk/Source/WebCore/Sources.txt

    r240315 r240328  
    953953editing/CompositeEditCommand.cpp
    954954editing/CreateLinkCommand.cpp
     955editing/CustomUndoStep.cpp
    955956editing/DeleteFromTextNodeCommand.cpp
    956957editing/DeleteSelectionCommand.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r240315 r240328  
    1513215132                F4E1965A21F2395000285078 /* JSUndoItemCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSUndoItemCustom.cpp; sourceTree = "<group>"; };
    1513315133                F4E1965F21F26E4E00285078 /* UndoItem.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = UndoItem.cpp; sourceTree = "<group>"; };
     15134                F4E1966121F27D3C00285078 /* CustomUndoStep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomUndoStep.h; sourceTree = "<group>"; };
     15135                F4E1966221F27D3D00285078 /* CustomUndoStep.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CustomUndoStep.cpp; sourceTree = "<group>"; };
    1513415136                F4E57EDA213F3F5F004EA98E /* FontAttributeChanges.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontAttributeChanges.h; sourceTree = "<group>"; };
    1513515137                F4E57EDF213F434A004EA98E /* WebCoreNSFontManagerExtras.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebCoreNSFontManagerExtras.h; sourceTree = "<group>"; };
     
    2110821110                                D0B0556709C6700100307E43 /* CreateLinkCommand.cpp */,
    2110921111                                D0B0556609C6700100307E43 /* CreateLinkCommand.h */,
     21112                                F4E1966221F27D3D00285078 /* CustomUndoStep.cpp */,
     21113                                F4E1966121F27D3C00285078 /* CustomUndoStep.h */,
    2111021114                                93309D8F099E64910056E581 /* DeleteFromTextNodeCommand.cpp */,
    2111121115                                93309D90099E64910056E581 /* DeleteFromTextNodeCommand.h */,
  • trunk/Source/WebCore/editing/CustomUndoStep.cpp

    r240327 r240328  
    2424 */
    2525
    26 #pragma once
     26#include "config.h"
     27#include "CustomUndoStep.h"
    2728
     29#include "Document.h"
     30#include "UndoItem.h"
     31#include "UndoManager.h"
    2832#include "VoidCallback.h"
    29 #include <wtf/IsoMalloc.h>
    30 #include <wtf/RefCounted.h>
    31 #include <wtf/WeakPtr.h>
    32 #include <wtf/text/WTFString.h>
    3333
    3434namespace WebCore {
    3535
    36 class Document;
    37 class UndoManager;
     36CustomUndoStep::CustomUndoStep(UndoItem& item)
     37    : m_undoItem(makeWeakPtr(item))
     38{
     39}
    3840
    39 class UndoItem : public RefCounted<UndoItem> {
    40     WTF_MAKE_ISO_ALLOCATED(UndoItem);
    41 public:
    42     struct Init {
    43         String label;
    44         RefPtr<VoidCallback> undo;
    45         RefPtr<VoidCallback> redo;
    46     };
     41void CustomUndoStep::unapply()
     42{
     43    if (!isValid())
     44        return;
    4745
    48     static Ref<UndoItem> create(Init&& init)
    49     {
    50         return adoptRef(*new UndoItem(WTFMove(init)));
    51     }
     46    // FIXME: It's currently unclear how input events should be dispatched when unapplying or reapplying custom
     47    // edit commands. Should the page be allowed to specify a target in the DOM for undo and redo?
     48    Ref<UndoItem> protectedUndoItem(*m_undoItem);
     49    protectedUndoItem->document()->updateLayoutIgnorePendingStylesheets();
     50    protectedUndoItem->undoHandler().handleEvent();
     51}
    5252
    53     bool isValid() const;
    54     void invalidate();
     53void CustomUndoStep::reapply()
     54{
     55    if (!isValid())
     56        return;
    5557
    56     Document* document() const;
     58    Ref<UndoItem> protectedUndoItem(*m_undoItem);
     59    protectedUndoItem->document()->updateLayoutIgnorePendingStylesheets();
     60    protectedUndoItem->redoHandler().handleEvent();
     61}
    5762
    58     void setUndoManager(UndoManager*);
    59 
    60     const String& label() const { return m_label; }
    61     VoidCallback& undoHandler() const { return m_undoHandler.get(); }
    62     VoidCallback& redoHandler() const { return m_redoHandler.get(); }
    63 
    64 private:
    65     UndoItem(Init&& init)
    66         : m_label(WTFMove(init.label))
    67         , m_undoHandler(init.undo.releaseNonNull())
    68         , m_redoHandler(init.redo.releaseNonNull())
    69     {
    70     }
    71 
    72     String m_label;
    73     Ref<VoidCallback> m_undoHandler;
    74     Ref<VoidCallback> m_redoHandler;
    75     WeakPtr<UndoManager> m_undoManager;
    76 };
     63bool CustomUndoStep::isValid() const
     64{
     65    return m_undoItem && m_undoItem->isValid();
     66}
    7767
    7868} // namespace WebCore
  • trunk/Source/WebCore/editing/CustomUndoStep.h

    r240327 r240328  
    2626#pragma once
    2727
    28 #include "VoidCallback.h"
    29 #include <wtf/IsoMalloc.h>
    30 #include <wtf/RefCounted.h>
     28#include "UndoStep.h"
     29#include <wtf/Ref.h>
    3130#include <wtf/WeakPtr.h>
    32 #include <wtf/text/WTFString.h>
    3331
    3432namespace WebCore {
    3533
    3634class Document;
    37 class UndoManager;
     35class UndoItem;
    3836
    39 class UndoItem : public RefCounted<UndoItem> {
    40     WTF_MAKE_ISO_ALLOCATED(UndoItem);
     37class CustomUndoStep final : public UndoStep {
    4138public:
    42     struct Init {
    43         String label;
    44         RefPtr<VoidCallback> undo;
    45         RefPtr<VoidCallback> redo;
    46     };
    47 
    48     static Ref<UndoItem> create(Init&& init)
     39    static Ref<CustomUndoStep> create(UndoItem& item)
    4940    {
    50         return adoptRef(*new UndoItem(WTFMove(init)));
     41        return adoptRef(*new CustomUndoStep(item));
    5142    }
    5243
     44private:
     45    CustomUndoStep(UndoItem&);
     46
     47    void unapply() final;
     48    void reapply() final;
     49    EditAction editingAction() const final { return EditAction::Unspecified; }
     50
    5351    bool isValid() const;
    54     void invalidate();
    5552
    56     Document* document() const;
    57 
    58     void setUndoManager(UndoManager*);
    59 
    60     const String& label() const { return m_label; }
    61     VoidCallback& undoHandler() const { return m_undoHandler.get(); }
    62     VoidCallback& redoHandler() const { return m_redoHandler.get(); }
    63 
    64 private:
    65     UndoItem(Init&& init)
    66         : m_label(WTFMove(init.label))
    67         , m_undoHandler(init.undo.releaseNonNull())
    68         , m_redoHandler(init.redo.releaseNonNull())
    69     {
    70     }
    71 
    72     String m_label;
    73     Ref<VoidCallback> m_undoHandler;
    74     Ref<VoidCallback> m_redoHandler;
    75     WeakPtr<UndoManager> m_undoManager;
     53    WeakPtr<UndoItem> m_undoItem;
    7654};
    7755
  • trunk/Source/WebCore/editing/EditingStyle.cpp

    r239535 r240328  
    4646#include "NodeTraversal.h"
    4747#include "QualifiedName.h"
     48#include "RenderElement.h"
    4849#include "RenderStyle.h"
    4950#include "StyleFontSizeFunctions.h"
  • trunk/Source/WebCore/editing/InsertEditableImageCommand.cpp

    r238708 r240328  
    5050
    5151    m_imageElement = HTMLImageElement::create(document());
    52     m_imageElement->setAttributeWithoutSynchronization(x_apple_editable_imageAttr, emptyAtom());
    53     m_imageElement->setAttributeWithoutSynchronization(widthAttr, AtomicString("100%", AtomicString::ConstructFromLiteral));
    54     m_imageElement->setAttributeWithoutSynchronization(heightAttr, AtomicString("300px", AtomicString::ConstructFromLiteral));
    55     m_imageElement->setAttributeWithoutSynchronization(styleAttr, AtomicString("display: block", AtomicString::ConstructFromLiteral));
     52    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::x_apple_editable_imageAttr, emptyAtom());
     53    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString("100%", AtomicString::ConstructFromLiteral));
     54    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString("300px", AtomicString::ConstructFromLiteral));
     55    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::styleAttr, AtomicString("display: block", AtomicString::ConstructFromLiteral));
    5656
    5757    insertNodeAt(*m_imageElement, endingSelection().start());
  • trunk/Source/WebCore/page/UndoItem.h

    r240315 r240328  
    3737class UndoManager;
    3838
    39 class UndoItem : public RefCounted<UndoItem> {
     39class UndoItem : public RefCounted<UndoItem>, public CanMakeWeakPtr<UndoItem> {
    4040    WTF_MAKE_ISO_ALLOCATED(UndoItem);
    4141public:
Note: See TracChangeset for help on using the changeset viewer.