Source

Kephra / lib / ded / Kephra / Document.pm

Full commit
use v5.12;
use warnings;

package Kephra::Document;
use Kephra::API qw(:log app_window);
use File::Spec;
use Cwd;

my $max_title_width = 22;

sub new {
	my ($class, $file) = @_;
	error('call me only from DocumentStash') if ( caller(0) )[0] ne 'Kephra::DocumentStash';
	my $self = bless ({ }, $class);
	$self->{'editor'} = Kephra::App::Editor->new();
	$self->load_file($file) if defined $file;

	return $self;
}

sub load_file {
	my $self = shift;
	my $file = shift;
	if (defined $file and -e $file){
		Kephra::DocumentStash::subscribe_file($self, $file, $self->{'file_path'});
		$self->{'file_path'} = $file;
		my @path_parts = File::Spec->splitpath( $file );
		$self->{'file_dir'} = $path_parts[1];
		$self->{'file_name'} = $path_parts[2];
		my $title = $self->{'file_name'};
		$title = substr( $title, 0, $max_title_width - 2 ) . '..'
			if length($title) > $max_title_width and $max_title_width > 7;
		$self->{'title'} = $title;

		my ($content, $encoding) = Kephra::IO::LocalFile::read( $file );
		$self->{'editor'}->reset( $content );
		app_window->SetStatusText( $encoding, 1);
		$self->{'encoding'} = $encoding;

		# rename docbar title
		#Kephra::API::doc_panel->set_page_title($self->{'title'}, $self->{'editor'});
		return 1;
	}
}
sub reset {
	my $self = shift;
	my $ed = $self->{'editor'};
	Kephra::DocumentStash::subscribe_file($self, '', $self->{'file_path'});
	$self->{'file_path'} = $self->{'file_dir'} = $self->{'file_name'} = '';
	$self->{'encoding'} = 'utf8';
	$ed->reset('');
	Kephra::DocumentStash::get_anon_title($self);
	#Kephra::API::doc_bar->set_page_title($self->{'title'}, $self->{'editor'});
	app_window->SetStatusText( $self->{'encoding'}, 1);
	app_window->refresh_title();
	Kephra::API::focus_back();
	return $self;
}

sub rename_file {
	my $self = shift;
}

sub saved { not shift->{'editor'}->GetModify()}

1;


__END__

attributes:
editor  Kephra::App::Editor
panel 
encoding
file_path
file_dir
file_name


planed attr:
ID? content selection notes syntax_mode history
readonly active_config encoding EOL tab_size tab_use     
caret_pos edit_pos folded_lines marked_lines

sub:
focus insert_text append_text