-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1178 from stevencohn/1143-copy-as-text
Add Copy as Text command
- Loading branch information
Showing
15 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//************************************************************************************************ | ||
// Copyright © 2023 Steven M Cohn. All rights reserved. | ||
//************************************************************************************************ | ||
|
||
namespace River.OneMoreAddIn.Commands | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using River.OneMoreAddIn.Models; | ||
|
||
|
||
/// <summary> | ||
/// Copy the page or selected content as plan text onto the system clipboard | ||
/// </summary> | ||
internal class CopyAsTextCommand : Command | ||
{ | ||
public CopyAsTextCommand() | ||
{ | ||
} | ||
|
||
|
||
public override async Task Execute(params object[] args) | ||
{ | ||
using var one = new OneNote(out var page, out var _); | ||
var cursor = page.GetTextCursor(); | ||
|
||
if (// cursor is not null if selection range is empty | ||
cursor != null && | ||
// selection range is a single line containing a hyperlink | ||
!(page.SelectionSpecial && page.SelectionScope == SelectionScope.Empty)) | ||
{ | ||
await CopyPageAsText(one, page); | ||
} | ||
else | ||
{ | ||
// if only images are selected and no text content then copy entire page... | ||
|
||
var other = page.Root.Descendants().Where(e => | ||
e.Attribute("selected")?.Value == "all" && | ||
e.Name.LocalName != "Image"); | ||
|
||
if (other.Any()) | ||
{ | ||
// some text was found, maybe with one or more images | ||
var clipboard = new ClipboardProvider(); | ||
await clipboard.Copy(); | ||
await clipboard.SetText(await clipboard.GetText()); | ||
} | ||
else | ||
{ | ||
// no range selection or only an image was selected | ||
await CopyPageAsText(one, page); | ||
} | ||
} | ||
} | ||
|
||
|
||
private async Task CopyPageAsText(OneNote one, Page page) | ||
{ | ||
var updated = one.GetPage(OneNote.PageDetail.Basic); | ||
var ns = updated.Root.GetNamespaceOfPrefix(OneNote.Prefix); | ||
|
||
updated.Root.Elements(ns + "Outline").ForEach(e => | ||
{ | ||
e.SetAttributeValue("selected", "all"); | ||
}); | ||
|
||
await one.Update(updated); | ||
|
||
var clipboard = new ClipboardProvider(); | ||
await clipboard.Copy(); | ||
await clipboard.SetText(await clipboard.GetText()); | ||
|
||
await one.Update(page); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters