Question related to selectedText in Context Menu

Hi,

I did the tutorial “Use highlighted text in Confluence Forge app” described here: https://developer.atlassian.com/platform/forge/create-confluence-contextmenu-module/

First, I recognized that the following code ist not correct

import ForgeUI, { render, ContextMenu, InlineDialog, Text, Strong, useProductContext } from '@forge/ui';

const App = () => {
  const { extensionContext: { selectedText } } = useProductContext();

  return (
      <InlineDialog>
        <Text><Strong>Selected text</Strong></Text>
        <Text>selectedText</Text>
      </InlineDialog>
  );
};

export const run = render(
    <ContextMenu><App/></ContextMenu>
);

It leads to this result
37

It needs to be {selectedText} to be correct.
However, this is just a small issue. What I want to ask is if it is possible to provide the selected text not trimmed and ignoring line breaks but the actual selectedText, meaning HTML or wiki style?

If I select a text containing a line break it removes the line break completely and shows the words directly after each other:

Also, if you have a space at beginning or end of the selected text it gets trimmed and displayed without those spaces.

Is it already possible to get the actual selected string or is it planned to change that?

@ij-s thanks for the heads up on the {selectedText}. I’ve submitted a pull request to the docs to include the curly braces. :slight_smile:

Re: the stripping/trimming (spaces, breaks, etc.), I wasn’t able to find a way to get an unstripped version of the selected text; however, I’ll ping the team to see if I’m missing something.

1 Like

@ij-s Unfortunately, there is not currently a way to fetch a non-stripped version of the selected text. I suggest submitting a feature request here along with the purpose / use case.

@nmansilla thanks for the update. I created a feature request: [FRGE-240] Provide non-stripped version of Selected Text for Context Menu Component in Confluence - Ecosystem Jira

It is a pity that it is not possible so far. It is a blocking point for many features to implement as you don’t get the actual selected text. If this would be the case everyone could decide whether to trim it in the apps code by oneself or not. As it is now, you always get the trimmed version and can’t change that.

1 Like