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

Add filename and date to screenshot file if not Untitled #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const P_TITLE = 'Polacode 📸'
function activate(context) {
const htmlPath = path.resolve(context.extensionPath, 'webview/index.html')

let lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop/code.png'))
let lastUsedImageUri
let lastFile
Comment on lines -19 to +20
Copy link
Author

Choose a reason for hiding this comment

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

  • Set lastUserImageUri to undefined until it is assigned for the first time during the call to the shoot event.
  • Added variable to contain data on the last updated TextDocument object.

let panel

vscode.window.registerWebviewPanelSerializer('polacode', {
Expand Down Expand Up @@ -74,7 +75,7 @@ function activate(context) {
case 'shoot':
vscode.window
.showSaveDialog({
defaultUri: lastUsedImageUri,
defaultUri: getFileName(),
Comment on lines -77 to +78
Copy link
Author

Choose a reason for hiding this comment

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

  • Call to the getFileName function instead of using the global variable as it becomes a derived value.

filters: {
Images: ['png']
}
Expand Down Expand Up @@ -119,9 +120,28 @@ function activate(context) {
})
}

function getFileName() {
let fileName = 'code.png'
if (!lastFile.isUntitled) {
const file = lastFile.uri.path.replace(/.*\/(.+[\..+]*)?(\..+)?$/, '$1$2')
const dateString = () => {
const date = new Date()
return date.toLocaleString()
.replace(/\//g, '-')
.replace(/,/, ' at')
.replace(/:/g, '.')
.replace(/(\d+)-(\d+)-(\d+)/, '$3-$1-$2')
}
fileName = `code - ${file} ${dateString()}.png`
}
lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop', fileName))
return lastUsedImageUri
}

Comment on lines +123 to +140
Copy link
Author

Choose a reason for hiding this comment

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

  • Computes the default file name with both the referenced document title and date appended.
  • Prefix of code remains to identify the document as a Polacode export.
  • Fallback to the original name code.png if the selected document is Untitled.
  • Set the global lastUsedImageUri variable for use.

function setupSelectionSync() {
return vscode.window.onDidChangeTextEditorSelection(e => {
if (e.selections[0] && !e.selections[0].isEmpty) {
lastFile = e.textEditor.document
Copy link
Author

Choose a reason for hiding this comment

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

  • Monitor document selection during each update.

vscode.commands.executeCommand('editor.action.clipboardCopyWithSyntaxHighlightingAction')
panel.postMessage({
type: 'update'
Expand Down