Grafoscopio/src/Grafoscopio/GrafoscopioNotebook.class.st

617 lines
17 KiB
Smalltalk

"
I am a Grafoscopio Notebook.
Example:
| testTree nb |
testTree := GrafoscopioNode new becomeDefaultTestTree.
nb := GrafoscopioNotebook new.
nb notebookContent: testTree.
nb openWithSpec
"
Class {
#name : #GrafoscopioNotebook,
#superclass : #ComposableModel,
#instVars : [
'tree',
'header',
'body',
'windowMainMenu',
'workingFile',
'notebook'
],
#category : #'Grafoscopio-UI'
}
{ #category : #utility }
GrafoscopioNotebook class >> SHA1For: aFile is: aSHA1String [
"I verify that a file has the same signature that the one in a given string,
returning true in that case or false otherwise"
^ (SHA1 new hashMessage: aFile asFileReference binaryReadStream contents) hex = aSHA1String
]
{ #category : #specs }
GrafoscopioNotebook class >> defaultSpec [
"comment stating purpose of message"
^ SpecLayout composed
newColumn: [:tcol|
tcol newRow: [ :wrow | wrow add: #windowMainMenu ] height: (self toolbarHeight);
newRow: [:row |
row newColumn: [ :tc |
tc add: #tree
] width: 300.
row newColumn: [ :bc |
bc newRow: [ :bcr | bcr add: #header ] height: self toolbarHeight.
bc add: #body ]]]
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> addNode [
| addedNode |
addedNode := tree highlightedItem content addNodeAfterMe.
self notebookContent: notebook.
]
{ #category : #operation }
GrafoscopioNotebook >> autoSaveBodyOf: aNode [
body body class = TextModel
ifTrue: [ body body whenTextChanged: [ :arg | aNode body: arg ] ].
body body class = GlamourPresentationModel
ifTrue: [ | playground |
playground := body body glmPres.
playground
when: GLMContextChanged
do: [ :ann |
ann property = #text
ifTrue: [ | playgroundText |
playgroundText := (playground pane ports at: 2) value.
aNode body: playgroundText ]
]
]
]
{ #category : #accessing }
GrafoscopioNotebook >> body [
^ body
]
{ #category : #accessing }
GrafoscopioNotebook >> body: anObject [
body := anObject
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> copyNodeToClipboard [
tree highlightedItem content copyToClipboard.
self notebookContent: notebook.
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> cutNodeToClipboard [
self copyNodeToClipboard; removeNode.
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> demoteNode [
| editedNode |
editedNode := tree highlightedItem content.
editedNode demote.
self notebookContent: notebook.
]
{ #category : #persistence }
GrafoscopioNotebook >> exportAsMarkdown [
"I save the current tree/document to a file"
| markdownFile |
workingFile
ifNil: [ self inform: 'File NOT exported. Please save the notebook on hard drive first' ]
ifNotNil: [
markdownFile := (((workingFile parent) / workingFile basenameWithoutExtension) fullName, '.markdown') asFileReference.
markdownFile exists ifTrue: [ markdownFile delete ].
markdownFile ensureCreateFile.
markdownFile writeStreamDo: [:stream | stream nextPutAll: self notebook asMarkdown].
"[ self exportAsMarkdown: self notebook on: ( markdownFile writeStream )]
ensure: [ (markdownFile writeStream) ifNotNil: #close ]."
self inform: ('File exported as: ', String cr, markdownFile fullName)]
]
{ #category : #persistence }
GrafoscopioNotebook >> exportAsSton: aNotebook on: aFileStream [
| stonPrettyString |
aNotebook flatten.
stonPrettyString := String streamContents: [ :stream |
(STON writer on: stream)
newLine: String crlf;
prettyPrint: true;
keepNewLines: true;
nextPut: aNotebook children].
aFileStream nextPutAll: stonPrettyString
]
{ #category : #api }
GrafoscopioNotebook >> extent [
^900@500
]
{ #category : #accessing }
GrafoscopioNotebook >> header [
^ header
]
{ #category : #accessing }
GrafoscopioNotebook >> header: anObject [
header := anObject
]
{ #category : #initialization }
GrafoscopioNotebook >> initializePresenter [
tree whenHighlightedItemChanged: [ :item |
tree highlightedItem ifNotNil: [self updateBodyFor: item]].
tree whenTreeUpdated: [ :item | item ifNotNil: [self updateBodyFor: item]].
header whenTextChanged: [ :arg |
(tree highlightedItem content header) = arg
ifFalse: [
(tree highlightedItem) content header: arg.
tree roots: tree roots.
self updateForSpecialHeader]].
]
{ #category : #initialization }
GrafoscopioNotebook >> initializeWidgets [
windowMainMenu := self newWindowMainMenu.
header := self newTextInput.
body := self newText.
body disable.
body text: '<- Select a node'.
tree := TreeModel new.
tree
childrenBlock: [:node | node children];
displayBlock: [:node | node title ].
self focusOrder
add: tree;
add: header;
add: body.
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> moveNodeAfter [
| editedNode |
editedNode := tree selectedItem content.
editedNode moveAfter.
self notebookContent: notebook
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> moveNodeBefore [
| editedNode |
editedNode := tree highlightedItem content.
editedNode moveBefore.
self notebookContent: notebook
]
{ #category : #initialization }
GrafoscopioNotebook >> newWindowMainMenu [
^MenuModel new
addGroup: [ :group |
group addItem: [ :item |
item
name: 'Notebook';
icon: Smalltalk ui icons smallObjectsIcon;
subMenu: self notebookSubMenu ].
group addItem: [ :item |
item
name: 'Project';
icon: Smalltalk ui icons catalogIcon;
subMenu: self projectSubMenu ] ];
addGroup: [ :group |
group addItem: [ :item |
item
name: nil;
description: 'Save notebook';
icon: Smalltalk ui icons smallSaveIcon;
action: [ self saveWorkingNotebook ] ].
group addItem: [ :item |
item
name: nil;
description: 'Cut';
icon: Smalltalk ui icons smallCutIcon;
action: [ self cutNodeToClipboard ] ].
group addItem: [ :item |
item
name: nil;
description: 'Copy';
icon: Smalltalk ui icons smallCopyIcon;
action: [ self copyNodeToClipboard ] ].
group addItem: [ :item |
item
name: nil;
description: 'Paste';
icon: Smalltalk ui icons smallPasteIcon;
action: [ self pasteNodeFromClipboard ] ]];
addGroup: [ :group |
group addItem: [ :item |
item
name: nil;
description: 'Add nodo';
icon: MendaIcons new plusIcon;
action: [ self addNode ] ].
group addItem: [ :item |
item
name: nil;
description: 'Delete node';
icon: MendaIcons new minusIcon;
action: [ self removeNode ] ].
group addItem: [ :item |
item
name: nil;
description: 'Move up node';
icon: MendaIcons new arrowUpIcon;
action: [ self moveNodeBefore ] ].
group addItem: [ :item |
item
name: nil;
description: 'Move down node';
icon: MendaIcons new arrowDownIcon;
action: [ self moveNodeAfter ] ].
group addItem: [ :item |
item
name: nil;
description: 'Move node left';
icon: MendaIcons new arrowLeftIcon;
action: [ self promoteNode ] ].
group addItem: [ :item |
item
name: nil;
description: 'Move node right';
icon: MendaIcons new arrowRightIcon;
action: [ self demoteNode ] ]];
addGroup: [ :group |
group addItem: [ :item |
item
name: nil;
description: 'Togle: code <--> text';
icon: MendaIcons new smalltalkCodeIcon;
action: [ self toggleCodeNode ] ].
group addItem: [ :item |
item
name: nil;
description: 'Tag as...';
icon: MendaIcons new tagAddIcon;
action: [ self inform: 'To be implemented...' ] ].
group addItem: [ :item |
item
name: nil;
description: 'Untag ....';
icon: MendaIcons new tagMinusIcon;
action: [ self inform: 'To be implemented...' ] ].
group addItem: [ :item |
item
name: nil;
description: 'Edit tags...';
icon: FontAwesomeIcons new tagsIcon;
action: [ self inform: 'To be implemented...' ] ]].
]
{ #category : #accessing }
GrafoscopioNotebook >> notebook [
^ notebook
]
{ #category : #accessing }
GrafoscopioNotebook >> notebook: anObject [
notebook := anObject
]
{ #category : #api }
GrafoscopioNotebook >> notebookContent: aTree [
| nodeBlock |
nodeBlock:= [:gfcNode | |node|
node := TreeNodeModel new.
node
hasChildren: [ gfcNode children isNotEmpty ];
children: [ gfcNode children collect: [:subNode | nodeBlock value: subNode ]];
content: gfcNode].
tree roots: (aTree children collect:[ :gfcNode | nodeBlock value: gfcNode])
]
{ #category : #initialization }
GrafoscopioNotebook >> notebookSubMenu [
^ MenuModel new
addGroup: [ :group |
group addItem: [ :item |
item
name: 'Save';
icon: Smalltalk ui icons smallSaveIcon;
shortcut: $s command;
action: [ self saveWorkingNotebook ] ].
group addItem: [ :item |
item
name: 'Save as...';
icon: Smalltalk ui icons smallSaveAsIcon;
action: [ self saveToFileUI ] ].
group addItem: [ :item |
item
name: 'Export as markdown';
icon: Smalltalk ui icons smallSaveAsIcon;
action: [ self exportAsMarkdown ] ].
group addItem: [ :item |
item
name: 'Export as html';
icon: Smalltalk ui icons smallWindowIcon;
action: [ self inform: 'To be implemented...' ] ].
group addItem: [ :item |
item
name: 'Export as pdf';
icon: Smalltalk ui icons smallPrintIcon;
action: [ self inform: 'To be implemented...' ] ].
group addItem: [ :item |
item
name: 'See html';
icon: Smalltalk ui icons smallInspectItIcon;
action: [ self inform: 'To be implemented...' ] ].
group addItem: [ :item |
item
name: 'See pdf';
icon: Smalltalk ui icons smallInspectItIcon;
action: [ self inform: 'To be implemented...' ] ] ]
]
{ #category : #persistence }
GrafoscopioNotebook >> openDefault [
"I open a new default notebook"
| nb |
nb := self class new.
nb
notebook: (GrafoscopioNode new becomeDefaultTree);
title: ' New | Grafoscopio notebook';
notebookContent: nb notebook.
^ nb openWithSpec.
]
{ #category : #persistence }
GrafoscopioNotebook >> openFromFile: aFileName [
"I open a notebook from a file named aFileName containing a grafoscopio tree"
self workingFile: aFileName.
self notebook: ((STON fromString: self workingFile contents) at: 1) parent.
self title: self workingFile basenameWithIndicator, ' | Grafoscopio notebook'.
self notebookContent: self notebook.
^ self openWithSpec.
]
{ #category : #persistence }
GrafoscopioNotebook >> openFromFileSelector [
| fileStream nb |
fileStream := UITheme builder
fileOpen: 'Choose a file'
extensions: #('ston').
fileStream ifNil: [
self inform: 'No file selected'.
^ self ].
self workingFile: fileStream name asFileReference.
nb := self class new.
nb openFromFile: self workingFile.
GrafoscopioGUI updateRecentNotebooksWith: workingFile
]
{ #category : #persistence }
GrafoscopioNotebook >> openFromUrl: anUrl [
"Opens a tree from a file named aFileName"
| fileName |
fileName := (anUrl splitOn: '/') last.
GrafoscopioGUI
downloadingFrom: anUrl
withMessage: 'Downloading document...'
into: FileLocator temp.
self class new openFromFile: (FileLocator temp / fileName)
]
{ #category : #persistence }
GrafoscopioNotebook >> openFromUrlUI [
"This method generates the UI for the openFromUrl: method, it asks for a URL from the user"
| fileUrl |
"GrafoscopioBrowser configureSettings."
fileUrl := UIManager default
textEntry: 'Ingrese la URL'
title: 'Nuevo documento desde URL'.
fileUrl isNil ifTrue: [ ^nil ].
self class new openFromUrl: fileUrl
]
{ #category : #persistence }
GrafoscopioNotebook >> openTutorial [
| tutorial gfcDocs |
gfcDocs := GrafoscopioDocumentation current.
tutorial := (gfcDocs localPlace fullName, '/', (gfcDocs documents at: 1)) asFileReference.
tutorial exists
ifTrue: [self class new openFromFile: tutorial]
ifFalse: [ GrafoscopioGUI updateDocumentationUI ]
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> pasteNodeFromClipboard [
tree highlightedItem content pasteFromClipboard.
self notebookContent: notebook.
]
{ #category : #initialization }
GrafoscopioNotebook >> projectSubMenu [
^ MenuModel new
addGroup: [ :group |
group addItem: [ :item |
item
name: 'Activate remote repository...';
icon: Smalltalk ui icons smallPushpinIcon;
action: [ self inform: 'To be implemented ...' ] ].
group addItem: [ :item |
item
name: 'Activate local repository...';
icon: Smalltalk ui icons homeIcon;
action: [ self inform: 'To be implemented ...' ] ].
group addItem: [ :item |
item
name: 'Add file...';
icon: Smalltalk ui icons newerPackagesAvailableIcon;
action: [ self inform: 'To be implemented ...' ] ].
group addItem: [ :item |
item
name: 'Delete file...';
icon: Smalltalk ui icons packageDeleteIcon;
action: [ self inform: 'To be implemented ...' ] ].
group addItem: [ :item |
item
name: 'Commit to repository';
icon: Smalltalk ui icons smallScreenshotIcon;
action: [ self inform: 'To be implemented ...' ] ].
group addItem: [ :item |
item
name: 'Credentials';
icon: Smalltalk ui icons userIcon;
action: [ self inform: 'To be implemented ...' ] ] ]
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> promoteNode [
| editedNote |
editedNote := tree selectedItem content.
editedNote promote.
self notebookContent: notebook
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> removeNode [
| contentToDelete parentContent newSelectedContent children |
contentToDelete := tree selectedItem content.
parentContent := contentToDelete parent.
children := parentContent children.
children size > 1
ifTrue: [
children last = contentToDelete
ifTrue: [ newSelectedContent := children at: (children size - 1) ]
]
ifFalse: [ newSelectedContent := parentContent ].
contentToDelete parent removeNode: contentToDelete.
self notebookContent: notebook
]
{ #category : #persistence }
GrafoscopioNotebook >> saveToFile: aFileReference [
"I save the current tree/document to a file"
aFileReference ifNil: [ self inform: 'No file selected for saving. Save NOT done'. ^ self ].
workingFile := aFileReference.
self workingFile exists ifTrue: [self workingFile delete].
self workingFile ensureCreateFile.
[ self exportAsSton: self notebook on: (self workingFile writeStream)]
ensure: [ (self workingFile writeStream) ifNotNil: #close ].
self title: self workingFile basenameWithIndicator, ' | Grafoscopio notebook'.
self inform: ('File saved at: ', String cr, self workingFile fullName).
GrafoscopioGUI updateRecentNotebooksWith: aFileReference.
]
{ #category : #persistence }
GrafoscopioNotebook >> saveToFileUI [
| file |
file := UITheme builder
fileSave: 'Export notebook to file as...'
extensions: #('ston')
path: nil.
file
ifNil: [ self inform: 'Export cancelled'. ^ self ]
ifNotNil:[self saveToFile: file].
]
{ #category : #persistence }
GrafoscopioNotebook >> saveWorkingNotebook [
"Saves the current tree to the user predefined file location used when he/she opened it."
self workingFile
ifNil: [ self saveToFileUI ]
ifNotNil: [ self saveToFile: workingFile ].
GrafoscopioGUI updateRecentNotebooksWith: workingFile
]
{ #category : #'editing nodes' }
GrafoscopioNotebook >> toggleCodeNode [
| currentNode |
currentNode := tree highlightedItem.
(currentNode content tags = 'código')
ifTrue: [ currentNode content tagAs: '' ]
ifFalse: [ currentNode content tagAs: 'código' ].
self updateBodyFor: currentNode.
]
{ #category : #accessing }
GrafoscopioNotebook >> tree [
^ tree
]
{ #category : #accessing }
GrafoscopioNotebook >> tree: anObject [
tree := anObject
]
{ #category : #operation }
GrafoscopioNotebook >> updateBodyFor: aNodeContainer [
| aNode |
self needRebuild: false.
tree needRebuild: false.
body needRebuild: true.
aNode := aNodeContainer content.
header text: aNode header.
body := self instantiate: aNode specModelClass new.
body content: aNode body.
self autoSaveBodyOf: aNode.
self buildWithSpecLayout: self class defaultSpec
]
{ #category : #operation }
GrafoscopioNotebook >> updateForSpecialHeader [
"I see if a node header is an url located at 'http://ws.stfx.eu', wich means that is a shared
workspace, and convert the node body to an interactive playground"
| currentNode nodeContent |
currentNode := tree highlightedItem.
currentNode ifNil: [ ^ self ].
nodeContent := currentNode content.
nodeContent header isAsciiString ifFalse: [ ^ self ].
nodeContent header asUrl host = 'ws.stfx.eu' ifFalse: [ ^ self ].
nodeContent
body: (ZnClient new get: nodeContent header);
tagAs: 'código'.
self updateBodyFor: currentNode
]
{ #category : #accessing }
GrafoscopioNotebook >> windowMainMenu [
^ windowMainMenu
]
{ #category : #accessing }
GrafoscopioNotebook >> windowMainMenu: anObject [
windowMainMenu := anObject
]
{ #category : #accessing }
GrafoscopioNotebook >> workingFile [
^ workingFile
]
{ #category : #accessing }
GrafoscopioNotebook >> workingFile: aFile [
workingFile := aFile.
]