Skip to content

Commit

Permalink
Mutable JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
sciyoshi committed Oct 11, 2023
1 parent 5f5ce01 commit c61cf39
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions prosemirror/model/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
cast,
)

from prosemirror.utils import JSON, text_length
from prosemirror.utils import MutableJSONList, text_length

if TYPE_CHECKING:
from prosemirror.model.schema import Schema
Expand Down Expand Up @@ -244,7 +244,7 @@ def find_index(self, pos: int, round: int = -1) -> Dict[str, int]:
i += 1
cur_pos = end

def to_json(self) -> JSON:
def to_json(self) -> Optional[MutableJSONList]:
if self.content:
return [item.to_json() for item in self.content]
return None
Expand Down
10 changes: 7 additions & 3 deletions prosemirror/model/mark.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
from typing import TYPE_CHECKING, Any, Final, List, Optional, cast

from prosemirror.utils import JSONDict
from prosemirror.utils import JSONDict, MutableJSONDict

if TYPE_CHECKING:
from .schema import MarkType, Schema
Expand Down Expand Up @@ -50,8 +51,11 @@ def eq(self, other: "Mark") -> bool:
return True
return self.type.name == other.type.name and self.attrs == other.attrs

def to_json(self) -> JSONDict:
return {"type": self.type.name, "attrs": self.attrs}
def to_json(self) -> MutableJSONDict:
result: MutableJSONDict = {"type": self.type.name}
if self.attrs:
result["attrs"] = cast(MutableJSONDict, copy.deepcopy(self.attrs))
return result

@classmethod
def from_json(
Expand Down
16 changes: 8 additions & 8 deletions prosemirror/model/node.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, TypedDict, Union
import copy
from typing import TYPE_CHECKING, Any, Callable, List, Optional, TypedDict, Union, cast

from typing_extensions import TypeGuard

from prosemirror.utils import JSON, JSONDict, text_length
from prosemirror.utils import JSONDict, MutableJSONDict, text_length

from .comparedeep import compare_deep
from .fragment import Fragment
Expand Down Expand Up @@ -306,11 +307,10 @@ def iteratee(node: "Node", offset: int, index: int) -> None:

return self.content.for_each(iteratee)

def to_json(self) -> JSONDict:
obj: Dict[str, JSON] = {"type": self.type.name}
for _ in self.attrs:
obj["attrs"] = self.attrs
break
def to_json(self) -> MutableJSONDict:
obj: MutableJSONDict = {"type": self.type.name}
if self.attrs:
obj["attrs"] = cast(MutableJSONDict, copy.deepcopy(self.attrs))
if getattr(self.content, "size", None):
obj["content"] = self.content.to_json()
if len(self.marks):
Expand Down Expand Up @@ -407,7 +407,7 @@ def eq(self, other: Node) -> bool:

def to_json(
self,
) -> JSONDict:
) -> MutableJSONDict:
return {**super().to_json(), "text": self.text}


Expand Down
6 changes: 3 additions & 3 deletions prosemirror/model/replace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, cast

from prosemirror.utils import JSON, JSONDict
from prosemirror.utils import JSONDict, MutableJSONDict

from .fragment import Fragment

Expand Down Expand Up @@ -85,10 +85,10 @@ def eq(self, other: "Slice") -> bool:
def __str__(self) -> str:
return f"{self.content}({self.open_start},{self.open_end})"

def to_json(self) -> JSON:
def to_json(self) -> Optional[MutableJSONDict]:
if not self.content.size:
return None
json = {"content": self.content.to_json()}
json: MutableJSONDict = {"content": self.content.to_json()}
if self.open_start > 0:
json["openStart"] = self.open_start
if self.open_end > 0:
Expand Down
9 changes: 8 additions & 1 deletion prosemirror/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Mapping, Sequence, Union
from typing import Mapping, MutableMapping, MutableSequence, Sequence, Union

from typing_extensions import TypeAlias

Expand All @@ -7,6 +7,13 @@

JSON: TypeAlias = Union[JSONDict, JSONList, str, int, float, bool, None]

MutableJSONDict: TypeAlias = MutableMapping[str, "MutableJSON"]
MutableJSONList: TypeAlias = MutableSequence["MutableJSON"]

MutableJSON: TypeAlias = Union[
MutableJSONDict, MutableJSONList, str, int, float, bool, None
]


def text_length(text: str) -> int:
return len(text.encode("utf-16-le")) // 2

0 comments on commit c61cf39

Please sign in to comment.