                (if (eq (org-element-property :todo-type context) 'done)
                    (or (car (+org-get-todo-keywords-for (org-element-property :todo-keyword context)))
                        'todo)
-                 'done)))
-             (t
-              (+org--refresh-inline-images-in-subtree)
-              (org-clear-latex-preview)
-              (org-latex-preview '(4)))))
+                 'done))))
+       ;; Update any metadata or inline previews in this subtree
+       (org-update-checkbox-count)
+       (let (org-hierarchical-todo-statistics)
+         (org-update-parent-todo-statistics))
+       (when (and (fboundp 'toc-org-insert-toc)
+                  (member "TOC" (org-get-tags)))
+         (toc-org-insert-toc)
+         (message "Updating table of contents"))
+       (let* ((beg (if (org-before-first-heading-p)
+                       (line-beginning-position)
+                     (save-excursion (org-back-to-heading) (point))))
+              (end (if (org-before-first-heading-p)
+                       (line-end-position)
+                     (save-excursion (org-end-of-subtree) (point))))
+              (overlays (ignore-errors (overlays-in beg end)))
+              (latex-overlays
+               (cl-find-if (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
+                           overlays))
+              (image-overlays
+               (cl-find-if (lambda (o) (overlay-get o 'org-image-overlay))
+                           overlays)))
+         (+org--toggle-inline-images-in-subtree beg end)
+         (if (or image-overlays latex-overlays)
+             (org-clear-latex-preview beg end)
+           (org--latex-preview-region beg end))))
 
       (`clock (org-clock-update-time-maybe))
 

If on a:
- checkbox list item or todo heading: toggle it.
- clock: update its time.
- headline: toggle latex fragments and inline images underneath.
- footnote reference: jump to the footnote's definition
- footnote definition: jump to the first reference of this footnote
- table-row or a TBLFM: recalculate the table's formulas

            type (org-element-type context)))
    (pcase type
      (`headline
       (cond ((memq (bound-and-true-p org-goto-map)
                    (current-active-maps))
              (org-goto-ret))
             ((and (fboundp 'toc-org-insert-toc)
                   (member "TOC" (org-get-tags)))
