Fix overwrite surround (#2247)

* Revert "Revert to setFormat() to fix color setting"

This reverts commit 613b5c1034.

* Fix overwrite-surround
This commit is contained in:
Henrik Giesel 2022-12-09 01:10:58 +01:00 committed by GitHub
parent 3413115be7
commit 6d0a242a1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 17 deletions

View file

@ -9,7 +9,7 @@ import { boolMatcher } from "./match-type";
import { splitPartiallySelected } from "./split-text"; import { splitPartiallySelected } from "./split-text";
import type { SurroundFormat } from "./surround-format"; import type { SurroundFormat } from "./surround-format";
function surroundInner<T>( function buildAndApply<T>(
node: Node, node: Node,
buildFormat: BuildFormat<T>, buildFormat: BuildFormat<T>,
applyFormat: ApplyFormat<T>, applyFormat: ApplyFormat<T>,
@ -19,30 +19,24 @@ function surroundInner<T>(
return buildFormat.recreateRange(); return buildFormat.recreateRange();
} }
function reformatInner<T>( function surroundOnCorrectNode<T>(
range: Range, range: Range,
base: Element, base: Element,
build: BuildFormat<T>, build: BuildFormat<T>,
apply: ApplyFormat<T>, apply: ApplyFormat<T>,
matcher: Matcher, matcher: Matcher,
): Range { ): Range {
const farthestMatchingAncestor = findFarthest( const node = findFarthest(
range.commonAncestorContainer, range.commonAncestorContainer,
base, base,
matcher, matcher,
); ) ?? range.commonAncestorContainer;
if (farthestMatchingAncestor) { return buildAndApply(node, build, apply);
return surroundInner(farthestMatchingAncestor, build, apply);
} else {
return surroundInner(range.commonAncestorContainer, build, apply);
}
} }
/** /**
* Assumes that there are no matching ancestor elements above * Will surround the entire range, removing any contained formatting nodes in the process.
* `range.commonAncestorContainer`. Make sure that the range is not placed
* inside the format before using this.
*/ */
export function surround<T>( export function surround<T>(
range: Range, range: Range,
@ -52,7 +46,7 @@ export function surround<T>(
const splitRange = splitPartiallySelected(range); const splitRange = splitPartiallySelected(range);
const build = new BuildFormat(format, base, range, splitRange); const build = new BuildFormat(format, base, range, splitRange);
const apply = new ApplyFormat(format); const apply = new ApplyFormat(format);
return surroundInner(range.commonAncestorContainer, build, apply); return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
} }
/** /**
@ -66,7 +60,7 @@ export function reformat<T>(
const splitRange = splitPartiallySelected(range); const splitRange = splitPartiallySelected(range);
const build = new ReformatBuildFormat(format, base, range, splitRange); const build = new ReformatBuildFormat(format, base, range, splitRange);
const apply = new ReformatApplyFormat(format); const apply = new ReformatApplyFormat(format);
return reformatInner(range, base, build, apply, boolMatcher(format)); return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
} }
export function unsurround<T>( export function unsurround<T>(
@ -77,5 +71,5 @@ export function unsurround<T>(
const splitRange = splitPartiallySelected(range); const splitRange = splitPartiallySelected(range);
const build = new UnsurroundBuildFormat(format, base, range, splitRange); const build = new UnsurroundBuildFormat(format, base, range, splitRange);
const apply = new UnsurroundApplyFormat(format); const apply = new UnsurroundApplyFormat(format);
return reformatInner(range, base, build, apply, boolMatcher(format)); return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
} }

View file

@ -8,7 +8,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { getPlatformString } from "@tslib/shortcuts"; import { getPlatformString } from "@tslib/shortcuts";
import { removeStyleProperties } from "@tslib/styling"; import { removeStyleProperties } from "@tslib/styling";
import { singleCallback } from "@tslib/typing"; import { singleCallback } from "@tslib/typing";
import { setFormat } from "editor/old-editor-adapter";
import { onMount } from "svelte"; import { onMount } from "svelte";
import IconButton from "../../components/IconButton.svelte"; import IconButton from "../../components/IconButton.svelte";
@ -110,7 +109,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
removeFormats.update((formats) => [...formats, namedFormat]); removeFormats.update((formats) => [...formats, namedFormat]);
function setTextColor(): void { function setTextColor(): void {
setFormat("foreColor", transformedColor); surrounder.overwriteSurround(key);
} }
const setCombination = "F7"; const setCombination = "F7";