Skip to content

Commit

Permalink
live editing of the file because ITS COOL
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Mar 15, 2024
1 parent 79abf4c commit 3182560
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 38 deletions.
98 changes: 67 additions & 31 deletions src/cascadia/TerminalApp/MarkdownPaneContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ namespace winrt::TerminalApp::implementation
WUX::Documents::Run currentRun{ nullptr };
TerminalApp::CodeBlock currentCodeBlock{ nullptr };
};

WUX::Controls::TextBlock makeDefaultTextBlock()
{
WUX::Controls::TextBlock b{};
b.IsTextSelectionEnabled(true);
b.TextWrapping(WUX::TextWrapping::WrapWholeWords);
return b;
}
int md_parser_enter_block(MD_BLOCKTYPE type, void* detail, void* userdata)
{
MyMarkdownData* data = reinterpret_cast<MyMarkdownData*>(userdata);
Expand All @@ -44,8 +50,7 @@ namespace winrt::TerminalApp::implementation
case MD_BLOCK_H:
{
MD_BLOCK_H_DETAIL* headerDetail = reinterpret_cast<MD_BLOCK_H_DETAIL*>(detail);
data->current = WUX::Controls::TextBlock{};
data->current.IsTextSelectionEnabled(true);
data->current = makeDefaultTextBlock();
const auto fontSize = std::max(16u, 36u - ((headerDetail->level - 1) * 6u));
data->current.FontSize(fontSize);
data->current.FontWeight(Windows::UI::Text::FontWeights::Bold());
Expand Down Expand Up @@ -121,8 +126,7 @@ namespace winrt::TerminalApp::implementation

if (data->current == nullptr)
{
data->current = WUX::Controls::TextBlock();
data->current.IsTextSelectionEnabled(true);
data->current = makeDefaultTextBlock();
data->root.Children().Append(data->current);
}
if (data->currentRun == nullptr)
Expand Down Expand Up @@ -191,8 +195,7 @@ namespace winrt::TerminalApp::implementation
{
if (const auto& curr{ data->current })
{
data->current = WUX::Controls::TextBlock();
data->current.IsTextSelectionEnabled(true);
data->current = makeDefaultTextBlock();
data->root.Children().Append(data->current);
}

Expand Down Expand Up @@ -223,6 +226,8 @@ namespace winrt::TerminalApp::implementation
case MD_TEXT_NORMAL:
default:
{
data->currentCodeBlock = nullptr;

auto run = data->currentRun ? data->currentRun : WUX::Documents::Run{};
run.Text(str);
if (data->current)
Expand All @@ -231,8 +236,7 @@ namespace winrt::TerminalApp::implementation
}
else
{
WUX::Controls::TextBlock block{};
block.IsTextSelectionEnabled(true);
WUX::Controls::TextBlock block = makeDefaultTextBlock();
block.Inlines().Append(run);
data->root.Children().Append(block);
data->current = block;
Expand Down Expand Up @@ -313,34 +317,42 @@ namespace winrt::TerminalApp::implementation
}
// BLINDLY TREATING TEXT AS utf-8 (I THINK)
std::string markdownContents{ buffer, read };
winrt::hstring fileContents = winrt::to_hstring(markdownContents);

Editing(false);
PropertyChanged.raise(*this, WUX::Data::PropertyChangedEventArgs{ L"Editing" });
FileContents(winrt::to_hstring(markdownContents));
PropertyChanged.raise(*this, WUX::Data::PropertyChangedEventArgs{ L"FileContents" });

_renderFileContents();
}
void MarkdownPaneContent::_renderFileContents()
{
// Was the file a .md file?
if (_filePath.ends_with(L".md"))
{
_loadMarkdown(fileContents);
_loadMarkdown();
}
else
{
_loadText(fileContents);
_loadText();
}
}
void MarkdownPaneContent::_loadText(const winrt::hstring& fileContents)
void MarkdownPaneContent::_loadText()
{
auto block = WUX::Controls::TextBlock();
block.IsTextSelectionEnabled(true);
block.FontFamily(WUX::Media::FontFamily{ L"Cascadia Code" });
block.Text(fileContents);
block.Text(FileContents());

RenderedMarkdown().Children().Append(block);
}

void MarkdownPaneContent::_loadMarkdown(const winrt::hstring& fileContents)
void MarkdownPaneContent::_loadMarkdown()
{
MyMarkdownData data;
data.page = this;

const auto parseResult = parseMarkdown(fileContents, data);
const auto parseResult = parseMarkdown(FileContents(), data);

if (0 == parseResult)
{
Expand All @@ -350,27 +362,51 @@ namespace winrt::TerminalApp::implementation

void MarkdownPaneContent::_loadTapped(const Windows::Foundation::IInspectable&, const Windows::UI::Xaml::Input::TappedRoutedEventArgs&)
{
auto p = FilePathInput().Text();
if (p != _filePath)
_filePath = FilePathInput().Text();
// Does the file exist? if not, bail
const wil::unique_handle file{ CreateFileW(_filePath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr) };
if (!file)
{
_filePath = p;
// Does the file exist? if not, bail
const wil::unique_handle file{ CreateFileW(_filePath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr) };
if (!file)
{
return;
}
return;
}

// It does. Clear the old one
_clearOldNotebook();
_loadFile();
}

// It does. Clear the old one
void MarkdownPaneContent::_editTapped(const Windows::Foundation::IInspectable&, const Windows::UI::Xaml::Input::TappedRoutedEventArgs&)
{
if (Editing())
{
_clearOldNotebook();
_loadFile();
_renderFileContents();

EditIcon().Glyph(L"\xe932"); // Label

_scrollViewer().Visibility(WUX::Visibility::Visible);
_editor().Visibility(WUX::Visibility::Collapsed);

Editing(false);
}
else
{
EditIcon().Glyph(L"\xe890"); // View

_scrollViewer().Visibility(WUX::Visibility::Collapsed);
_editor().Visibility(WUX::Visibility::Visible);

Editing(true);
}
PropertyChanged.raise(*this, WUX::Data::PropertyChangedEventArgs{ L"Editing" });
}
void MarkdownPaneContent::_reloadTapped(const Windows::Foundation::IInspectable&, const Windows::UI::Xaml::Input::TappedRoutedEventArgs&)

void MarkdownPaneContent::_closeTapped(const Windows::Foundation::IInspectable&, const Windows::UI::Xaml::Input::TappedRoutedEventArgs&)
{
// Clear the old one
_clearOldNotebook();
_loadFile();
// // Clear the old one
// _clearOldNotebook();
// _loadFile();
CloseRequested.raise(*this, nullptr);
}

void MarkdownPaneContent::_handleRunCommandRequest(const TerminalApp::CodeBlock& sender,
Expand Down
15 changes: 12 additions & 3 deletions src/cascadia/TerminalApp/MarkdownPaneContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ namespace winrt::TerminalApp::implementation
MarkdownPaneContent();
MarkdownPaneContent(const winrt::hstring& filePath);

til::property<bool> Editing{ false };
til::property<winrt::hstring> FileContents{ L"" };

void SetLastActiveControl(const Microsoft::Terminal::Control::TermControl& control);

// TODO! this should just be til::property_changed_event but I don't havfe tht commit here
til::event<winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler> PropertyChanged;

#pragma region IPaneContent
winrt::Windows::UI::Xaml::FrameworkElement GetRoot();

Expand Down Expand Up @@ -53,16 +59,19 @@ namespace winrt::TerminalApp::implementation
friend struct MarkdownPaneContentT<MarkdownPaneContent>; // for Xaml to bind events

winrt::hstring _filePath{};
// winrt::hstring _fileContents{};

winrt::weak_ref<Microsoft::Terminal::Control::TermControl> _control{ nullptr };

void _clearOldNotebook();
void _loadFile();
void _loadText(const winrt::hstring& fileContents);
void _loadMarkdown(const winrt::hstring& fileContents);
void _renderFileContents();
void _loadText();
void _loadMarkdown();

void _loadTapped(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::TappedRoutedEventArgs& e);
void _reloadTapped(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::TappedRoutedEventArgs& e);
void _editTapped(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::TappedRoutedEventArgs& e);
void _closeTapped(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::TappedRoutedEventArgs& e);
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/cascadia/TerminalApp/MarkdownPaneContent.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import "CodeBlock.idl";

namespace TerminalApp
{
[default_interface] runtimeclass MarkdownPaneContent : Windows.UI.Xaml.Controls.UserControl, IPaneContent
[default_interface] runtimeclass MarkdownPaneContent : Windows.UI.Xaml.Controls.UserControl,
IPaneContent,
Windows.UI.Xaml.Data.INotifyPropertyChanged
{
MarkdownPaneContent();
MarkdownPaneContent(String originalPath);
void SetLastActiveControl(Microsoft.Terminal.Control.TermControl control);

Boolean Editing;
String FileContents;

event Windows.Foundation.TypedEventHandler<Object, Microsoft.Terminal.Settings.Model.ActionAndArgs> DispatchActionRequested;

}
Expand Down
29 changes: 26 additions & 3 deletions src/cascadia/TerminalApp/MarkdownPaneContent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@
Orientation="Horizontal">
<Button Margin="4"
Tapped="_loadTapped">
Load
<FontIcon FontFamily="Segoe UI, Segoe Fluent Icons, Segoe MDL2 Assets"
FontSize="12"
Glyph="&#xe8e5;" />
<!-- OpenFile -->
</Button>
<Button Margin="4"
Tapped="_editTapped">
<FontIcon x:Name="EditIcon"
FontFamily="Segoe UI, Segoe Fluent Icons, Segoe MDL2 Assets"
FontSize="12"
Glyph="&#xe932;" />
<!-- Label -->
</Button>
<Button Margin="4"
Tapped="_reloadTapped">
Tapped="_closeTapped">
<FontIcon FontFamily="Segoe UI, Segoe Fluent Icons, Segoe MDL2 Assets"
FontSize="12"
Glyph="&#xe72c;" />
Glyph="&#xe8bb;" />
<!-- ChromeClose -->
</Button>
</StackPanel>
</Grid>
Expand All @@ -61,6 +73,17 @@
VerticalAlignment="Stretch"
Background="#ff0000" />

<TextBox x:Name="_editor"
Grid.Column="1"
Padding="3"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AcceptsReturn="True"
FontFamily="Cascadia Code"
IsSpellCheckEnabled="False"
Text="{x:Bind FileContents, Mode=TwoWay}"
Visibility="{x:Bind Editing}" />

<ScrollViewer x:Name="_scrollViewer"
Grid.Column="1"
Padding="3"
Expand Down

1 comment on commit 3182560

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (12)
actiopn
BLOCKTYPE
havfe
ifying
SOFTBR
SPANTYPE
sxnui
taskpane
tbh
TEXTTYPE
tht
userdata
Previously acknowledged words that are now absent chcbpat DESTINATIONNAME inputrc kcub kcud kcuf kcuu khome Mbxy QUESTIONMARK reallocs reamapping RTFTo USERDATA WScript xff 🫥
To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the git@github.com:microsoft/terminal.git repository
on the dev/migrie/fhl/md-pane branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.22/apply.pl' |
perl - 'https://github.com/microsoft/terminal/actions/runs/8301225652/attempts/1'
Available 📚 dictionaries could cover words (expected and unrecognized) not in the 📘 dictionary

This includes both expected items (2245) from .github/actions/spelling/expect/04cdb9b77d6827c0202f51acd4205b017015bfff.txt
.github/actions/spelling/expect/alphabet.txt
.github/actions/spelling/expect/expect.txt
.github/actions/spelling/expect/web.txt and unrecognized words (12)

Dictionary Entries Covers Uniquely
cspell:k8s/dict/k8s.txt 153 2 2
cspell:swift/src/swift.txt 53 1 1
cspell:gaming-terms/dict/gaming-terms.txt 59 1 1
cspell:monkeyc/src/monkeyc_keywords.txt 123 1 1
cspell:cryptocurrencies/cryptocurrencies.txt 125 1 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/check-spelling@v0.0.22 in its with:

      with:
        extra_dictionaries:
          cspell:k8s/dict/k8s.txt
          cspell:swift/src/swift.txt
          cspell:gaming-terms/dict/gaming-terms.txt
          cspell:monkeyc/src/monkeyc_keywords.txt
          cspell:cryptocurrencies/cryptocurrencies.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/check-spelling@v0.0.22 in its with:

check_extra_dictionaries: ''
Errors (1)

See the 📜action log or 📝 job summary for details.

❌ Errors Count
❌ ignored-expect-variant 3

See ❌ Event descriptions for more information.

✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.