Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve detection when multiple special text are laid #31

Open
AlexV525 opened this issue Mar 10, 2021 · 1 comment
Open

Improve detection when multiple special text are laid #31

AlexV525 opened this issue Mar 10, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@AlexV525
Copy link
Member

AlexV525 commented Mar 10, 2021

In some cases that special texts are incomplete or stacked, the incomplete one might prevent another special text been created.

Consider the following case:

  • Text: #电子书 //<M 167328>@蒋菊</M>:转发 //<M 213288>@吴瀚</M>:转发
  • PoundText
    • start key: #
    • end key: #
  • MentionText
    • start key: <M>
    • end key: </M>

The first mention text will be started while the mention text is not ended, at this time the default builder didn't cut off the previous special text, which means there will be no new special text.

if (specialText != null) {
if (!specialText.isEnd(textStack)) {
specialText.appendContent(char);
} else {
inlineList.add(specialText.finishText());
specialText = null;
textStack = '';
}
} else {
specialText = createSpecialText(textStack,
textStyle: textStyle, onTap: onTap, index: i);
if (specialText != null) {
if (textStack.length - specialText.startFlag.length >= 0) {
textStack = textStack.substring(
0, textStack.length - specialText.startFlag.length);
if (textStack.isNotEmpty) {
inlineList.add(TextSpan(text: textStack, style: textStyle));
}
}
textStack = '';
}
}

To avoid such case and not breakable too much, there can be a little trick that split the create method to implement this.

// Split create method
void _createSpecialText(int index, {bool create = true}) {
  if (create) {
    specialText = createSpecialText(
      textStack,
      textStyle: textStyle,
      onTap: onTap,
      index: index,
    );
  }
  if (specialText != null) {
    if (textStack.length - specialText.startFlag.length >= 0) {
      textStack = textStack.substring(
        0,
        textStack.length - specialText.startFlag.length,
      );
      if (textStack.isNotEmpty) {
         inlineList.add(TextSpan(text: textStack, style: textStyle));
      }
    }
    textStack = '';
  }
}

for (int i = 0; i < data.length; i++) {
  final String char = data[i];
  textStack += char;
  if (specialText != null) {
    // Try to create a new special text first.
    final SpecialText _tempText = createSpecialText(
      textStack,
      textStyle: textStyle,
      onTap: onTap,
      index: i,
    );
    // Prepend the previous special text's start key, then drop the old special text.
    if (_tempText != null &&
        _tempText.runtimeType != specialText.runtimeType) {
      textStack = '${specialText.startFlag}$textStack';
      specialText = _tempText;
      _createSpecialText(i, create: false);
    } else {
      if (!specialText.isEnd(textStack)) {
        specialText.appendContent(char);
      } else {
        inlineList.add(specialText.finishText());
        specialText = null;
        textStack = '';
      }
    }
  } else {
    _createSpecialText(i);
  }
}
@AlexV525 AlexV525 added the enhancement New feature or request label Mar 10, 2021
@zmtzawqlp
Copy link
Member

final List<InlineSpan> children = <InlineSpan>[];
正则的应该可以实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants