|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 |
| -from typing import TYPE_CHECKING, Callable, Iterator, List |
| 5 | +from typing import TYPE_CHECKING, Callable, Iterator, List, cast |
6 | 6 |
|
7 | 7 | from docx.oxml.drawing import CT_Drawing
|
8 | 8 | from docx.oxml.ns import qn
|
| 9 | +from docx.oxml.parser import OxmlElement |
9 | 10 | from docx.oxml.simpletypes import ST_BrClear, ST_BrType
|
10 | 11 | from docx.oxml.text.font import CT_RPr
|
11 | 12 | from docx.oxml.xmlchemy import BaseOxmlElement, OptionalAttribute, ZeroOrMore, ZeroOrOne
|
@@ -87,6 +88,19 @@ def iter_items() -> Iterator[str | CT_Drawing | CT_LastRenderedPageBreak]:
|
87 | 88 |
|
88 | 89 | return list(iter_items())
|
89 | 90 |
|
| 91 | + def insert_comment_range_end_and_reference_below(self, comment_id: int) -> None: |
| 92 | + """Insert a `w:commentRangeEnd` and `w:commentReference` element after this run. |
| 93 | +
|
| 94 | + The `w:commentRangeEnd` element is the immediate sibling of this `w:r` and is followed by |
| 95 | + a `w:r` containing the `w:commentReference` element. |
| 96 | + """ |
| 97 | + self.addnext(self._new_comment_reference_run(comment_id)) |
| 98 | + self.addnext(OxmlElement("w:commentRangeEnd", attrs={qn("w:id"): str(comment_id)})) |
| 99 | + |
| 100 | + def insert_comment_range_start_above(self, comment_id: int) -> None: |
| 101 | + """Insert a `w:commentRangeStart` element with `comment_id` before this run.""" |
| 102 | + self.addprevious(OxmlElement("w:commentRangeStart", attrs={qn("w:id"): str(comment_id)})) |
| 103 | + |
90 | 104 | @property
|
91 | 105 | def lastRenderedPageBreaks(self) -> List[CT_LastRenderedPageBreak]:
|
92 | 106 | """All `w:lastRenderedPageBreaks` descendants of this run."""
|
@@ -132,6 +146,23 @@ def _insert_rPr(self, rPr: CT_RPr) -> CT_RPr:
|
132 | 146 | self.insert(0, rPr)
|
133 | 147 | return rPr
|
134 | 148 |
|
| 149 | + def _new_comment_reference_run(self, comment_id: int) -> CT_R: |
| 150 | + """Return a new `w:r` element with `w:commentReference` referencing `comment_id`. |
| 151 | +
|
| 152 | + Should look like this: |
| 153 | +
|
| 154 | + <w:r> |
| 155 | + <w:rPr><w:rStyle w:val="CommentReference"/></w:rPr> |
| 156 | + <w:commentReference w:id="0"/> |
| 157 | + </w:r> |
| 158 | +
|
| 159 | + """ |
| 160 | + r = cast(CT_R, OxmlElement("w:r")) |
| 161 | + rPr = r.get_or_add_rPr() |
| 162 | + rPr.style = "CommentReference" |
| 163 | + r.append(OxmlElement("w:commentReference", attrs={qn("w:id"): str(comment_id)})) |
| 164 | + return r |
| 165 | + |
135 | 166 |
|
136 | 167 | # ------------------------------------------------------------------------------------
|
137 | 168 | # Run inner-content elements
|
|
0 commit comments